Question

I am having some issues with a code that is occasionally and sporadically throwing the following exception:

boost interprocess: no such file or directory

There are multiple codes accessing the same set of files, but some of the codes will move the files around to different directories in real time. The codes handling and moving the files are using file locks, e.g.

boost::interprocess::file_lock

The process happening in code is the following: 1) Program 1 checks to see the file it wants to lock exists 2) If the above check passes, it then locks the file using file_lock

The problem, I think, is that between step 1 and 2, Program 2 can use boost::filesystem::rename on the file Program 1 is working on and move it.

If both programs are running simultaneously, is there any way to prevent this from happening?

Was it helpful?

Solution

Don't check if the file exists before locking. Instead, just attempt to lock it; if the file doesn't exist, Boost will throw an interprocess_exception alerting you that the file is locked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top