#include <tamer/lock.hh>
Public Member Functions | |
| mutex () | |
| Default constructor creates a mutex. | |
| void | acquire (const event<> &done) |
| Acquire the mutex for exclusive access. | |
| void | release () |
| Release a mutex acquired for exclusive access. | |
| void | acquire_shared (const event<> &done) |
| Acquire the mutex for shared access. | |
| void | release_shared () |
| Release a mutex acquired for shared access. | |
A mutex object is an event-based mutual-exclusion and/or read/write lock. Acquire the mutex with the acquire() method, which triggers its event<> argument when the mutex is acquired. Later, release it with the release() method.
The mutex class also provides shared access, as for a read lock. Acquire the mutex for shared access with the acquire_shared() method, and release it thereafter with the release_shared() method. More than one callback function can acquire_shared() a mutex at once, but all the shared acquirers must release_shared() the mutex before a subsequent acquire() will succeed.
Acquire requests are served strictly in order of their arrival. For example, the following code:
tamer::mutex m; tamed void exclusive(int which) { twait { m.acquire(make_event()); } printf("%d: acquired\n", which); twait { tamer::at_delay_sec(1, make_event()); } m.release(); printf("%d: released\n", which); } tamed void shared(int which) { twait { m.acquire_shared(make_event()); } printf("%d: acquired shared\n", which); twait { tamer::at_delay_sec(1, make_event()); } m.release_shared(); printf("%d: released shared\n", which); } int main(int argc, char *argv[]) { tamer::initialize(); exclusive(1); shared(2); shared(3); exclusive(4); shared(5); tamer::loop(); }
will generate this output:
1: acquired
1: released
2: acquired shared
3: acquired shared
2: released shared
3: released shared
4: acquired
4: released
5: acquired shared
5: released shared
| void tamer::mutex::acquire | ( | const event<> & | done | ) | [inline] |
Acquire the mutex for exclusive access.
| done | Event triggered when the mutex is acquired. |
| void tamer::mutex::release | ( | ) | [inline] |
Release a mutex acquired for exclusive access.
| void tamer::mutex::acquire_shared | ( | const event<> & | done | ) | [inline] |
Acquire the mutex for shared access.
| done | Event triggered when the mutex is acquired. |
| void tamer::mutex::release_shared | ( | ) | [inline] |
Release a mutex acquired for shared access.
1.5.1