2
0
mirror of https://github.com/boostorg/cobalt.git synced 2026-01-31 20:12:22 +00:00
Files
cobalt/doc/tour/entry.adoc
Klemens Morgenstern 45901641ac renamed to cobalt.
2023-10-16 21:42:07 +08:00

54 lines
930 B
Plaintext

== Entry into an cobalt environment
In order to use <<awaitable, awaitables>> we need to be able to `co_await` them, i.e. be within a coroutine.
We got four ways to achieve this:
<<main>>:: replace `int main` with a coroutine
[source,cpp]
----
cobalt::main co_main(int argc, char* argv[])
{
// co_await things here
co_return 0;
}
----
<<thread>>:: create a thread for the asynchronous environments
[source,cpp]
----
cobalt::thread my_thread()
{
// co_await things here
co_return;
}
int main(int argc, char ** argv[])
{
auto t = my_thread();
t.join();
return 0;
}
----
<<task>>:: create a task and run or spawn it
[source,cpp]
----
cobalt::task<void> my_thread()
{
// co_await things here
co_return;
}
int main(int argc, char ** argv[])
{
cobalt::run(my_task()); // sync
asio::io_context ctx;
cobalt::spawn(ctx, my_task(), asio::detached);
ctx.run();
return 0;
}
----