Notinlist
Notinlist

Reputation: 16640

C++: File lock between threads and processes

I would like to create file based locking that ensures exclusive access of an associated resource. The boost:interprocess::file_lock is good for separating processes, but not good for separating threads. That problem can be solved with an additional boost::recursive_mutex if we extract the file names to unique full paths (eg. no relatively specified lock files like something/x.lock) and associate the mutexes with them.

I would like to have an easier and relatively standard way of file based locking that work for threads and processes alike.

Upvotes: 3

Views: 1896

Answers (1)

Hans Passant
Hans Passant

Reputation: 941505

Use a recursive_named_mutex instead. Works across processes as well as thread. A lot cheaper and less error prone to boost.

Upvotes: 3

Related Questions