Ticket #8306: namedMutexExample.cpp

File namedMutexExample.cpp, 597 bytes (added by David Hebbeker <david.hebbeker@…>, 10 years ago)

Minimal example of using named mutexes and unlocking them several times consecutively. Linked with -lpthread. The program is expected to never return.

Line 
1#include <stdlib.h>
2#include <boost/interprocess/sync/named_mutex.hpp>
3
4int main(void) {
5
6 using namespace boost::interprocess;
7
8 named_mutex mutex(open_or_create, "mutX"); // This mutex is non-recursive
9 // the mutex should be unlocked initially
10
11 mutex.unlock(); // This should have no effect, as it already is unlocked
12 mutex.unlock(); // This should have no effect, as it already is unlocked
13
14 mutex.lock(); // Now the mutex should be locked
15 mutex.lock(); // This should never return, as the mutex is already locked!
16
17 return EXIT_FAILURE; // In case we arrived here, something went wrong
18}
19