does boost::asio co_spawn create an actual thread?
Asked Answered
H

1

4

When looking through boost asio co_spawn documentation (https://www.boost.org/doc/libs/1_78_0/doc/html/boost_asio/reference/co_spawn/overload6.html), I see this statement, "Spawn a new coroutined-based thread of execution", however my understanding is that co_spawn does not create an actual thread, but uses threads that are part of the boost::asio::io_context pool. It's a "coroutine-based thread of execution" in a sense, that this coroutine would be a root of all coroutines that are spawned from inside this one

Is my understanding correct here or an actual thread is created whenever co_spawn is used like this:

::boost::asio::co_spawn(io_ctx, [&] -> ::boost::asio::awaitable<void> {
 // do something
}, ::boost::asio::detached);

thanks!

Huff answered 1/2, 2022 at 11:32 Comment(0)
N
9

It does not. See The Proactor Design Pattern: Concurrency Without Threads and https://www.boost.org/doc/libs/1_78_0/doc/html/boost_asio/overview/core/threads.html

What does detached mean/do? The documentation says:

The detached_t class is used to indicate that an asynchronous operation is detached. That is, there is no completion handler waiting for the operation's result.

It comes down to writing a no-op handler but (a) less work (b) more room for the library to optimize.


Another angle to look at this from is this: if the execution context for the executor (io_ctx) is never run/polled, nothing will ever happen. As always in boost, you decide where you run the service (whether you use threads e.g.)

Narcolepsy answered 1/2, 2022 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.