// Sleep 10 seconds
co_await fv::Tasks::Delay (std::chrono::seconds (10));fv::IoContext &_ctx = fv::Tasks::GetContext ();Asynchronous mutex is a mutex suitable for asynchronous environments. In contrast to std::mutex, there are the following features:
- Supports asynchronous wait locking
- Locking and unlocking do not require the same thread
Create mutex
fv::AsyncMutex _mtx {}; // Pass the true argument to lock during initializationLock:
// try lock
bool _locked = _mtx.TryLock ();
// asynchronous lock
co_await _mtx.Lock ();
// asynchronous timeout lock
bool _locked = co_await _mtx.Lock (std::chrono::seconds (1));Unlock:
_mtx.Unlock ();To know if it is locked:
bool _locked = _mtx.IsLocked ();An asynchronous semaphore is a semaphore suitable for asynchrony. In contrast to the library's std::counting_semaphore, there are the following features:
- Supports asynchronous wait for acquire
Create semaphore:
fv::AsyncSemaphore _sema { 1 }; // Parameter means the initial number of resourcesAcquire resources:
// try acquire resources
bool _acq = _sema.TryAcquire ();
// asynchronous acquire resources
co_await _mtx.Acquire ();
// asynchronous timeout acquire resources
bool _acq = co_await _mtx.Acquire (std::chrono::seconds (1));Release resources:
_mtx.Release ();Get the count of available resources:
size_t _count = _mtx.GetResCount ();TODO