Async is a thread orchestration? (sometimes)

I finished reading this tokio tutorial which helped a lot
https://tokio.rs/tokio/tutorial/async

helped me understand

  • why futures are lazy
  • how each future takes control and reports

some futures require threads(?), I assume every future that calls the waker,
because they need to be executed continuously and need to be able to call the waker
even when they don't have control of the process.

so my question is, as the title suggests "async is a thread orchestration? (sometimes)"

No, the waker just needs to be called somehow — usually by some kind of callback mechanism. A significant part of the point of async is to enable concurrency that does not introduce new threads. You can have futures that manage threads (e.g. tokio::spawn_blocking is kind of like that) but most futures don't.

1 Like

As a specific example of a set of futures that don't need a thread each, consider Tokio's network I/O futures; these share an "I/O driver", which uses an event loop to wake futures. The event loop does not run on a thread of its own; one of the Tokio worker threads will run the event loop instead of going to sleep when there's no work to be done, and when an event comes through from the OS, it'll trigger the appropriate wakers.

This has no threads beyond the workers; if you have a single worker thread, then that thread is either running a future, waiting for events, or calling wakers in response to events (and expecting that a consequence of calling wakers is that there will be futures to run).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.