boost::capy::coro_lock

An asynchronous mutex for coroutines.

Synopsis

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

lock_awaiter

Awaiter returned by lock().

lock_guard

RAII lock guard for coro_lock.

lock_guard_awaiter

Awaiter returned by scoped_lock() that returns a lock_guard on resume.

Member Functions

Name

Description

coro_lock [constructor]

Constructors

operator= [deleted]

Copy assignment operator

is_locked

Returns true if the mutex is currently locked.

lock

Returns an awaiter that acquires the mutex.

scoped_lock

Returns an awaiter that acquires the mutex with RAII.

unlock

Releases the mutex.

Created with MrDocs