Search code examples
c++boostfile-iolockinginterprocess

boost interprocess file_lock does not work with multiple processes


I seem to be having an issue with boost::interprocess::file_lock

I have process 1 that is essentially

    boost::interprocess::file_lock test_lock("testfile.csv");
    test_lock.lock();
    sleep(1000);
    test_lock.unlock();

When I run a second process while the first process is sleeping, I find that I am still able to read testfile.csv. What's worse, I can even overwrite it.

Am I misinterpreting how file_lock works? I was under the impression that calling .lock() gives it exclusive lock over the file and prevents any other process from read/modifying the file.


Solution

  • file_lock is not for locking a file. It is a mutual exclusion object that uses a file as its backing technology. The contents of the file are basically irrelevant; what is relevant is that all instances of a file_lock pointing to that file will respect the locking characteristics of the lock.

    As with any mutex-type object, the lock itself is there to protect or otherwise meter access to some other resource.

    It has nothing to do with filesystem protection of files.

    Reference