libcoro  1.0
Coroutine support library for C++20
Static Public Member Functions | Friends | List of all members
coro::suspend Class Reference

Suspend current coroutine and switch to another coroutine ready to run. More...

#include <cooperative.h>

Inherits trace::suspend_always.

Static Public Member Functions

static void await_suspend (std::coroutine_handle<> h)
 this function is static. More...
 

Friends

bool in_cooperative_mode ()
 Determines, whether current thread is in cooperative mode. More...
 
void enqueue (prepared_coro c)
 Enqueue coroutine to the queue in the cooperative mode. More...
 

Detailed Description

Suspend current coroutine and switch to another coroutine ready to run.

This allows you to run cooperative task switching in the current thread. In order for this to work, additional corutines need to be scheduled to run within this thread. Each time a corutine is suspended on suspend(), execution is switched to the next scheduled corutine. The current corutine is scheduled to run at the end of the queue.

This mode must be activated. Activation must be done by the main coroutine, which first executes @b co_await suspend() to activate the cooperative run. This first @b co_await is not about waiting, as there is no corutine ready in the queue yet. After that, this main corutine can start other corutines that use @b co_await suspend() when they run. The main corutine itself can also use @b co_await suspend().

The main coroutine can finish anytime as it is no longer considered as main. The current thread stays in this mode while there are any suspended coroutine in the queue.

Usage:

co_await coro::suspend();
Suspend current coroutine and switch to another coroutine ready to run.
Definition: cooperative.h:44
Note
this queue is thread local. If you need to share a queue between threads, use thread_pool ,

Definition at line 44 of file cooperative.h.


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