2
0
mirror of https://github.com/boostorg/cobalt.git synced 2026-01-19 04:02:16 +00:00
Files
cobalt/doc/tour/entry.adoc
Klemens Morgenstern b807c700dc doc typo fixes
2026-01-11 07:27:47 +08:00

54 lines
927 B
Plaintext

== Entry into a 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;
}
----