Skip to content

Latest commit

 

History

History
93 lines (61 loc) · 1.61 KB

6_Other.md

File metadata and controls

93 lines (61 loc) · 1.61 KB

Another asynchronous functionality

Sleep task

// Sleep 10 seconds
co_await fv::Tasks::Delay (std::chrono::seconds (10));

If external needs io_context

fv::IoContext &_ctx = fv::Tasks::GetContext ();

Asynchronous mutex

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 initialization

Lock:

// 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 ();

Asynchronous semaphore

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 resources

Acquire 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 ();

Example

TODO