boost::capy::coro_lock
An asynchronous mutex for coroutines.
Synopsis
Declared in <boost/capy/ex/coro_lock.hpp>
class coro_lock;
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
coro_lock cm;
task<> protected_operation() {
co_await cm.lock();
// ... critical section ...
cm.unlock();
}
// Or with RAII:
task<> protected_operation() {
auto guard = co_await cm.scoped_lock();
// ... critical section ...
// unlocks automatically
}
Types
Name |
Description |
Awaiter returned by lock(). |
|
RAII lock guard for coro_lock. |
|
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