boost::capy::async_mutex

An asynchronous mutex for coroutines.

Synopsis

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

lock_awaiter

Awaiter returned by lock().

lock_guard

RAII lock guard for async_mutex.

lock_guard_awaiter

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

Member Functions

Name

Description

async_mutex [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