...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::interprocess::named_mutex
// In header: <boost/interprocess/sync/named_mutex.hpp> class named_mutex { public: // construct/copy/destruct named_mutex(create_only_t, const char *, const permissions & = permissions()); named_mutex(open_or_create_t, const char *, const permissions & = permissions()); named_mutex(open_only_t, const char *); named_mutex(create_only_t, const wchar_t *, const permissions & = permissions()); named_mutex(open_or_create_t, const wchar_t *, const permissions & = permissions()); named_mutex(open_only_t, const wchar_t *); ~named_mutex(); // public member functions void unlock(); void lock(); bool try_lock(); template<typename TimePoint> bool timed_lock(const TimePoint &); template<typename TimePoint> bool try_lock_until(const TimePoint &); template<typename Duration> bool try_lock_for(const Duration &); // public static functions static bool remove(const char *); static bool remove(const wchar_t *); };
A mutex with a global name, so it can be found from different processes. This mutex can't be placed in shared memory, and each process should have it's own named_mutex.
named_mutex
public
construct/copy/destructnamed_mutex(create_only_t, const char * name, const permissions & perm = permissions());
Creates a global mutex with a name. Throws interprocess_exception
on error.
named_mutex(open_or_create_t, const char * name, const permissions & perm = permissions());
Opens or creates a global mutex with a name. If the mutex is created, this call is equivalent to named_mutex(create_only_t, ... ) If the mutex is already created, this call is equivalent named_mutex(open_only_t, ... ) Does not throw
named_mutex(open_only_t, const char * name);
Opens a global mutex with a name if that mutex is previously created. If it is not previously created this function throws interprocess_exception
.
named_mutex(create_only_t, const wchar_t * name, const permissions & perm = permissions());
Creates a global mutex with a name. Throws interprocess_exception
on error.
Note: This function is only available on operating systems with native wchar_t APIs (e.g. Windows).
named_mutex(open_or_create_t, const wchar_t * name, const permissions & perm = permissions());
Opens or creates a global mutex with a name. If the mutex is created, this call is equivalent to named_mutex(create_only_t, ... ) If the mutex is already created, this call is equivalent named_mutex(open_only_t, ... ) Does not throw
Note: This function is only available on operating systems with native wchar_t APIs (e.g. Windows).
named_mutex(open_only_t, const wchar_t * name);
Opens a global mutex with a name if that mutex is previously created. If it is not previously created this function throws interprocess_exception
.
Note: This function is only available on operating systems with native wchar_t APIs (e.g. Windows).
~named_mutex();
Destroys *this and indicates that the calling process is finished using the resource. The destructor function will deallocate any system resources allocated by the system for use by this process for this resource. The resource can still be opened again calling the open constructor overload. To erase the resource from the system use remove().
named_mutex
public member functionsvoid unlock();
Unlocks a previously locked mutex.
void lock();
Requires: The calling thread does not own the mutex.
Locks the mutex, sleeps when the mutex is already locked. Throws interprocess_exception
if a severe error is found
Note: A program may deadlock if the thread that has ownership calls this function. If the implementation can detect the deadlock, an exception could be thrown.
bool try_lock();
Requires: The calling thread does not own the mutex.
Tries to lock the mutex, returns false when the mutex is already locked, returns true when success. Throws interprocess_exception
if a severe error is found
Note: A program may deadlock if the thread that has ownership calls this function. If the implementation can detect the deadlock, an exception could be thrown.
template<typename TimePoint> bool timed_lock(const TimePoint & abs_time);
Requires: The calling thread does not own the mutex.
Tries to lock the the mutex until time abs_time, Returns false when timeout expires, returns true when locks. Throws interprocess_exception
if a severe error is found
Note: A program may deadlock if the thread that has ownership calls this function. If the implementation can detect the deadlock, an exception could be thrown.
template<typename TimePoint> bool try_lock_until(const TimePoint & abs_time);
Same as timed_lock
, but this function is modeled after the standard library interface.
template<typename Duration> bool try_lock_for(const Duration & dur);
Same as timed_lock
, but this function is modeled after the standard library interface.
named_mutex
public static functionsstatic bool remove(const char * name);
Erases a named mutex from the system. Returns false on error. Never throws.
static bool remove(const wchar_t * name);
Erases a named mutex from the system. Returns false on error. Never throws.
Note: This function is only available on operating systems with native wchar_t APIs (e.g. Windows).