I want to switch from CreatMutex
to boost::interprocess::named_mutex
to limit my application to a single instance. Both methods works when the application runs and ends just fine. However, the lock is not released when the application crashes and using boost::interprocess::named_mutex
. I could resolve that issue by using two name_mutex but I don't really understand the issue.
Why is the lock for boost::interprocess::named_mutex
not released when the application crashes but it is release with CreatMutex
? What's the difference?
boost::interprocess::named_mutex mutex(boost::interprocess::open_or_create, "my_mutex");
boost::interprocess::scoped_lock<boost::interprocess::named_mutex> lock(mutex, boost::interprocess::try_to_lock);
if(!lock) {
return 1; //exit
}
//application may crash here.
boost::interprocess::named_mutex::remove("my_mutex");
return 1; //exit
Caveat: I've not spent much time with
boost::interprocess
, so this information is just from a quick inspection of the source. That said, I've used the Windows synchronisation API's a lot, so here goes...The main difference between the two methods of interprocess synchronisation is how the object exists within the system.
With
boost::interprocess::named_mutex
, as well as a system-specific mutex, it looks like a synchronisation object is created as a file on the system. The location of the file is based on Registry entries (see note 1) (at least in Boost 1.54.0)... it's most likely located under the Common Application Data folder (see note 2). When the aplication crashes, this file is, in your case, not removed. I'm not sure if this is by design... however in the case of an application crash, it's perhaps best not to mess with the file system, just in case.Conversely, when you use
CreateMutex
, an object is created at the kernel mode, which for named mutexes can be accessed by several applications. You get a handle to the Mutex by specifying the name when you create it, and you lose the handle when you callCloseHandle
on it. The mutex object is destroyed when there are no more handles referencing it.The important part of this is in the documentation:
This basically means that Windows will clean up after your application.
Note that if you don't perform a
ReleaseMutex
, and your application owns the mutex when it dies, then it's possible/likely that a waiting thread or process would see that the mutex had been abandoned (WaitForSingleObject
returnsWAIT_ABANDONED
), and would gain ownership.I apologise for not providing a solution, but I hope it answers your question about why the two systems act differently.
Just as an aside, using registry entries to get this information is horrible - it would be safer, and more future-proof, to use
SHGetKnownFolderPath
. But I digress.Depending on your OS version, this could be
%ALLUSERSPROFILE%\Application Data\boost.interprocess
orProgramData\boost.interprocess
, or somewhere else entirely.