FuturesUnordered Task Management

Trying to understand how FuturesUnordered works. Getting tripped up with how the ready_to_run_queue interacts with linked list. Specifically, confused by pending_next_all, spin_next_all, link, and unlink. Does anyone have a good reference as to how this works? Specific questions include:

  • The docs mention under heavy thread contention, a compare-exchange loop could become costly. What does thread contention mean here?

  • How does using stub resolve this? Confused as to how using pending_next_all allows next_all as needed for iterator. When will there be a case where head_all -> stub -> next?

  • More generally, would love an explanation about how ready_to_run_queue works with head_all, next_all pointers and stub. Particularly for the impl Stream block

How much do you know about writing futures manually? Do you understand how join_all works? Having this context will make writing an answer easier.

Hmm. Using join_all before, and have of course read bunch of poll implementations, but, perhaps clouded by recent experience with FuturesUnordered.

Edit: reading through poll implementation for JoinAll. Basic understanding is that for each ElemState, call poll. If any NotReady, return NotReady otherwise return Ready(result). Feel free to ask other questions to gauge understanding make it easier to write an answer.

Hmm. Using join_all before, and have of course read bunch of poll implementations, but, perhaps clouded by recent experience with FuturesUnordered.

Edit: reading through poll implementation for JoinAll. Basic understanding is that for each ElemState, call poll. If any NotReady, return NotReady otherwise return Ready(result). Feel free to ask other questions to gauge understanding make it easier to write an answer.

Is there a better place to ask this question. Happy redirect this question to a place where it may be more appropriate, but didn't see anything in the repo..

I haven't really had the time to check out the source again to remind myself of how it works. You could open an issue on the crate, or ask in e.g. Tokio's discord. That would increase the pool of people who can answer who sees it.

Okay thanks. As general procedure, should questions re futures-rs be redirected to Tokio discord? Try to avoid opening issues when possible..

I don't think futures has its own discord, but you are certainly welcome to ask them on Tokio's discord, as they are tools relevant to people using Tokio. You can also ask questions here.

Ok cool. Yeah their repo did not have a discussion section either, so will just defer to Tokio for the future. Thanks for being welcoming

If you search the history of #tokio-dev on the Tokio discord, I'm pretty sure you will find several discussions, because we have had some talk about adding it to Tokio proper.

Oh nice really familiar with discord -- will check into that thanks again
edit: here's the link to the discussion post

FuturesUnordered, unlike join_all, doesn't poll ALL tasks per poll. Instead, FuturesUnordered polls each task individually as the waker gets called for the specific task.

Here's a great in-depth source: https://cafbit.com/post/tokio_internals/

Note: spawning the async closure into the localset or threadpool also polls only on wakeups (i.e., via tokio::task::spawn or tokio::task::spawn_local)

This link provided does not help much in understanding the linked list, ready_to_run_queue, and spinning. The Tokio discussion post also did not get any responses. Next step is to try discord.

What is the proper channel to ask on the discord?

Decided to post in the off-topic channel