Async++ unknown
Async (co_await/co_return) code for C++
Loading...
Searching...
No Matches
asyncpp::mutex Class Reference

A mutex with an asynchronous lock() operation. More...

#include <asyncpp/mutex.h>

Classes

struct  lock_awaiter
 
struct  scoped_lock_awaiter
 

Public Member Functions

constexpr mutex () noexcept
 Construct mutex in its unlocked state.
 
constexpr mutex (decltype(construct_locked)) noexcept
 Construct mutex in its locked state.
 
 ~mutex ()
 Destruct mutex.
 
 mutex (const mutex &) noexcept=delete
 
 mutex (mutex &&) noexcept=delete
 
mutexoperator= (const mutex &) noexcept=delete
 
mutexoperator= (mutex &&) noexcept=delete
 
bool try_lock () noexcept
 Attempt to acquire the mutex whitout blocking or yielding.
 
constexpr lock_awaiter lock () noexcept
 Acquire the the mutex using co_await.
 
constexpr scoped_lock_awaiter lock_scoped () noexcept
 Acquire the the mutex using co_await and wrap it in a mutex_lock.
 
void unlock () noexcept
 Unlock the mutex.
 
bool is_locked () const noexcept
 Query if the lock is currently locked.
 

Static Public Attributes

struct { 
 
construct_locked 
 Tag value used to construct locked mutex.
 

Friends

class mutex_lock
 

Detailed Description

A mutex with an asynchronous lock() operation.

Provides a asynchronous lock() method that can be awaited to allow usage in coroutines, without blocking others. Unlike std::mutex, this mutex is not tied to a particular thread. This is usefull if the coroutine gets suspended while holding the lock and is resumed on a different thread. The implementation is lock-free and does not throw.

Constructor & Destructor Documentation

◆ ~mutex()

asyncpp::mutex::~mutex ( )
inline

Destruct mutex.

Note
If the mutex is destroyed while there are coroutines trying to lock it, the behaviour is undefined.

Member Function Documentation

◆ is_locked()

bool asyncpp::mutex::is_locked ( ) const
inlinenoexcept

Query if the lock is currently locked.

Warning
This is unreliable if the mutex is used in multiple preemtive threads.

◆ lock()

constexpr mutex::lock_awaiter asyncpp::mutex::lock ( )
inlineconstexprnoexcept

Acquire the the mutex using co_await.

Returns
An awaitable type that will try to lock the mutex once awaited and resume once it holds the mutex.

◆ lock_scoped()

constexpr mutex::scoped_lock_awaiter asyncpp::mutex::lock_scoped ( )
inlineconstexprnoexcept

Acquire the the mutex using co_await and wrap it in a mutex_lock.

Returns
An awaitable type that will try to lock the mutex once awaited and resume once it holds the mutex.

◆ try_lock()

bool asyncpp::mutex::try_lock ( )
inlinenoexcept

Attempt to acquire the mutex whitout blocking or yielding.

Returns
true The lock was aquired
false The lock could not be aquired (its already locked)

◆ unlock()

void asyncpp::mutex::unlock ( )
inlinenoexcept

Unlock the mutex.

Note
Behaviour is undefined if the mutex is not currently locked. Resumes one of the waiting coroutines (if any) on the current thread before it returns.

The documentation for this class was generated from the following file: