I need interprocess synchronization around a piece of equipment. Since this code will need to work on Windows and Linux, I wrap the Boost Interprocess mututex. Everything works well with my method of checking for mutex rejection. There is a possibility that this could happen, and therefore I must prepare for this.
I left the mutex in my testing and, of course, when I use scoped_lock to lock the mutex, the process blocks indefinitely. I realized that this is due to the use of the scoped_lock timeout mechanism (since the lot of time spent on Googling for accounting methods for this is actually not very much, the increase did not cost much because of mobility considerations).
Without further ado, here is what I have:
#include <boost/interprocess/sync/named_recursive_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
typedef boost::interprocess::named_recursive_mutex MyMutex;
typedef boost::interprocess::scoped_lock<MyMutex> ScopedLock;
MyMutex* pGate = reinterpret_cast<MyMutex*>(new MyMutex(boost::interprocess::open_or_create, "MutexName");
{
boost::posix_time::ptime timeout(boost::posix_time::microsec_clock::local_time() + boost::posix_time::seconds(10));
ScopedLock lock(*pGate, timeout);
if(!lock.owns()) {
delete pGate;
boost::interprocess::named_recursive_mutex::remove("MutexName");
pGate = reinterpret_cast<MyMutex*>(new MyMutex(boost::interprocess::open_or_create, "MutexName");
}
}
, , . :
- -, , ScopedLock . .
- -, , ScopedLock ctor , . , , , 10 , ?
- mutex , -, ScopedLock ctor , , , . , .
, ? , , , .
, - , , 10 , . , , , 50 60 , 10 - "" .
Windows 7 Visual Studio 2010.
,
Andy