The easiest way to get started with an cobalt application is to use the co_main
function with the following signature:
cobalt::main co_main(int argc, char *argv[]);
Declaring co_main
will add a main
function that performs all the necessary steps to run a coroutine on an event loop.
This allows us to write very simple asynchronous programs.
cobalt::main co_main(int argc, char *argv[])
{
auto exec = co_await cobalt::this_coro::executor; // (1)
asio::steady_timer tim{exec, std::chrono::milliseconds(50)}; // (2)
co_await tim.async_wait(cobalt::use_op); // (3)
co_return 0;
}
-
get the executor
main
running on -
Use it with an asio object
-
co_await
an cobalt operation
The main promise will create an asio::signal_set
and uses it for cancellation.
SIGINT
becomes total , while SIGTERM
becomes terminal cancellation.
Note
|
The cancellation will not be forwarded to detached coroutines. The user will need to take care to end then on cancellation, since the program otherwise doesn’t allow graceful termination. |
It will also create an asio::io_context
to run on, which you can get through the this_coro::executor
.
It will be assigned to the cobalt::this_thread::get_executor()
.
It also creates a memory resource that will be used as a default for internal memory allocations.
It will be assigned to the thread_local
to the cobalt::this_thread::get_default_resource()
.
Every coroutine has an internal state, called promise
(not to be confused with the cobalt::promise
).
Depending on the coroutine properties different things can be co_await
-ed, like we used in the example above.
They are implemented through inheritance, and shared among different promise types
The main promise has the following properties.