Mutual mutual error reinforcement and failure verification

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");

{
    // ScopedLock lock(*pGate); // this blocks indefinitely
    boost::posix_time::ptime timeout(boost::posix_time::microsec_clock::local_time() + boost::posix_time::seconds(10));
    ScopedLock lock(*pGate, timeout); // a 10 second timeout that returns immediately if the mutex is abandoned ?????
    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

+5
4

-, , ScopedLock ctor .

, boost . Boost . , linux . . rubust boost libs: http://boost.2283326.n4.nabble.com/boost-interprocess-gt-1-45-robust-mutexes-td3416151.html

.

stackoverflow: boost:: interprocess:: interprocess_mutex?

, , ScopedLock ctor , . , , , 10 , ?

, . : , , try. : http://www.boost.org/doc/libs/1_53_0/doc/html/boost/interprocess/scoped_lock.html#idp57421760-bb , - false.

inline bool windows_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
{
   sync_handles &handles =
      windows_intermodule_singleton<sync_handles>::get();
   //This can throw
   winapi_mutex_functions mut(handles.obtain_mutex(this->id_));
   return mut.timed_lock(abs_time);
}

, , .

, , ScopedLock ctor , , , . , .

, , . Linux, /dev/shm/MutexName. Linux , , , , . :: :: named_recursive_mutex:: .

+4

BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS. , mutexes interprocess , .

, . - interprocess (, message_queue), , . , .

, ; - .

+4

BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING . . , . . named_mutex. , , . , .

#define TIMEOUT 1000
#define SAFELOCK(pMutex) \
    boost::posix_time::ptime wait_time \
        = boost::posix_time::microsec_clock::universal_time() \
        + boost::posix_time::milliseconds(TIMEOUT); \
    boost::interprocess::scoped_lock<boost::interprocess::named_mutex> lock(*pMutex, wait_time); \
    if(!lock.owns()) { \
        pMutex->unlock(); }

, , , . . . . , lock.owns() .

+1

boost :: interprocess :: named_mutex 3 defination: mutex Windows mutex, , !

linux, boost pthread_mutex, 1_65_1version

interprocess_mutex , API (Windows Mutex linux pthread_mutex ), Mutex .

0
source

All Articles