Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Need clarification on file_lock documentation

Aperta
#52 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Documentazione
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
cpp

Direzione di ricerca

Iniziare dalla documentazione di boost::interprocess::file_lock e riprodurre il comportamento usando l'esempio C++ incluso nell'issue. Determinare se le indicazioni documentate su thread e processi siano errate o semplicemente poco chiare; il lavoro è terminato quando la questione relativa alla documentazione o all'implementazione ha una spiegazione risolta e verificata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

The documentation of boost::interprocess::file_lock has some cautions on synchronization limitations:

  • It's unspecified if a file_lock synchronizes two threads from the same process.
  • It's unspecified if a process can use two file_lock objects pointing to the same file.

But then gives the guidance:

  • For each file, use a single file_lock object per process.
  • Use the same thread to lock and unlock a file.

I then created the following example:
https://wandbox.org/permlink/dO2rRDCHVWBE3pGrhttps://wandbox.org/permlink/3CNfkA3RcMkwbd2v

#include <fstream> 
#include <iostream> 
#include <cstdlib> 
#include <boost/interprocess/sync/file_lock.hpp> 
#include <boost/interprocess/sync/scoped_lock.hpp> 
#include <boost/filesystem.hpp> 
#include <mutex> 
#include <thread> 
#include <chrono> 
int main() 
{ 
    std::ofstream("f.txt"); 
    boost::interprocess::file_lock lock("f.txt"); 
    std::mutex mtx; 
    const size_t max = 100; 
    size_t t1Count = 0; 
    std::thread 
        t1 
        ( 
            [&lock,&t1Count,&mtx,max]() 
            { 
                while(t1Count < max) 
                { 
                    //std::lock_guard<std::mutex> mtxLock(mtx); 
                    boost::interprocess::scoped_lock<boost::interprocess::file_lock> guard(lock); 
                    std::cout << "Number 1" << std::endl; 
                    t1Count++; 
                    std::this_thread::sleep_for(std::chrono::milliseconds(1)); 
                } 
            } 
        ); 
    size_t t2Count = 0; 
    std::thread 
        t2 
        ( 
            [&lock,&t2Count,&mtx,max]() 
            { 
                while(t2Count < max) 
                { 
                    //std::lock_guard<std::mutex> mtxLock(mtx); 
                    boost::interprocess::scoped_lock<boost::interprocess::file_lock> guard(lock); 
                    std::cout << "Number 2" << std::endl; 
                    t2Count++; 
                    std::this_thread::sleep_for(std::chrono::milliseconds(1)); 
                } 
            } 
        ); 
    t1.join(); 
    t2.join(); 
    return 0; 
} 

It only works properly if the std::mutex is used in addition to the file_lock. That was originally my thought when I read "It's unspecified if a file_lock synchronizes two threads from the same process.", but it seemed like I was following the guidance "Use the same thread to lock and unlock a file.". If I haven't messed anything up, I would probably update the documentation to say something like:
"If utilitizing the file_lock from multiple threads within the same process, synchronize access to the file_lock with an intraprocess synchronization mechanism such as a mutex."

Otherwise, is it a bug?

Thanks,
Rob

Lingua principale
C++
Stelle
185
Fork
131
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di boostorg/interprocess

Tutte le issue di boostorg/interprocess

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.