Yochai Timmer
Yochai Timmer

Reputation: 49251

interprocess::named_upgradable_mutex - remains locked if process is killed

I'm using boost::interprocess::named_upgradable_mutex to synchronize a few processes.

I'm using boost::interprocess::sharable_lock and boost::interprocess::scoped_lock to lock the mutex.

When testing the synchronization, it looks fine as long as the processes are working, and are closed normally.

But, I've notices that if a process is killed (via TaskManager for example) while holding the mutex, the mutex remains locked.

Any idea how i can handle process failures ?

I've thought about using timed_lock() just in case... any other ideas?

Upvotes: 3

Views: 1872

Answers (2)

Alex Fihman
Alex Fihman

Reputation: 9

In case you kill your app for some reason, you can unlock this lock either by logout from Windows, or by executing command mutex.unlock();

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182763

You're working on the symptom rather than the problem. The purpose of a mutex is to allow a process or thread to put shared data into an inconsistent state. If the process dies while holding the mutex, the shared data is still in an inconsistent state. The problem is how to return the shared data to a consistent state, not how to unlock the mutex.

When you return the shared data to a consistent state, include the mutex or lock in the data you return to a consistent state. The simplest way is to remove the existing lock and create a new one. You will likely have to do the same thing for the shared data.

If you really need to do this though, I'd suggest you're probably not using the right tool for the job.

Upvotes: 2

Related Questions