boost::capy::async_mutex
An asynchronous mutex for coroutines.
Synopsis
Declared in <boost/capy/ex/async_mutex.hpp>
class async_mutex;
Description
This mutex provides mutual exclusion for coroutines without blocking. When a coroutine attempts to acquire a locked mutex, it suspends and is added to an intrusive wait queue. When the holder unlocks, the next waiter is resumed with the lock held.
Zero Allocation
The wait queue node is embedded in the lock_awaiter, which lives on the coroutine frame during suspension. No heap allocation occurs.
Thread Safety
This mutex is NOT thread‐safe. It is designed for single‐threaded use where multiple coroutines may contend for a resource.
Example
async_mutex mutex;
task<> protected_operation() {
co_await mutex.lock();
// ... critical section ...
mutex.unlock();
}
// Or with RAII:
task<> protected_operation() {
auto guard = co_await mutex.scoped_lock();
// ... critical section ...
// unlocks automatically
}
Types
Name |
Description |
Awaiter returned by lock(). |
|
RAII lock guard for async_mutex. |
|
Awaiter returned by scoped_lock() that returns a lock_guard on resume. |
Member Functions
Name |
Description |
|
Constructors |
|
Copy assignment operator |
Returns true if the mutex is currently locked. |
|
Returns an awaiter that acquires the mutex. |
|
Returns an awaiter that acquires the mutex with RAII. |
|
Releases the mutex. |
Created with MrDocs