You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using spawn_many with a range iota | transform, or spawn_group with a regular for loop. I've been meaning to put together a parallel_for API, but it'll end up being a wrapper over one of those anyway.
If you want to range over an index only, and have the task capture other references, then you need to avoid the dangling capture problem. There are 3 solutions:
Construct, dispatch, and await the tasks in the same expression so the lambda is lifetime-extended (as in the skynet example above).
Create the lambda as a named lvalue first so its lifetime is guaranteed to exceed that of the tasks.
Pass the indexes into an outer capturing non-coroutine lambda that returns the task from an inner non-capturing coroutine which takes everything by argument. That looks like lines 57-61 here
If you want to dispatch functions instead of coroutines then use spawn_func_many. The most complete example set is in test_spawn_many / test_spawn_func_many
Unless you're looking to fork/join from a synchronous context - then it's post_bulk_waitable.
I don't like spawning possibly hundreds of thousands of equal tasks (feels like it would have quite some overhead being allocated, put in some queue, dequeued...) I use the following code to spawn (by default) 1 worker task per cpu and then use an atomic to get the next index to process. The callable can also take begin/end indices for chunk processing.
Activity
tzcnt commentedon Jun 23, 2026
Using spawn_many with a range iota | transform, or spawn_group with a regular for loop. I've been meaning to put together a parallel_for API, but it'll end up being a wrapper over one of those anyway.
If you want to range over an index only, and have the task capture other references, then you need to avoid the dangling capture problem. There are 3 solutions:
If you want to dispatch functions instead of coroutines then use spawn_func_many. The most complete example set is in test_spawn_many / test_spawn_func_many
Unless you're looking to fork/join from a synchronous context - then it's post_bulk_waitable.
dixlorenz commentedon Jun 27, 2026
I don't like spawning possibly hundreds of thousands of equal tasks (feels like it would have quite some overhead being allocated, put in some queue, dequeued...) I use the following code to spawn (by default) 1 worker task per cpu and then use an atomic to get the next index to process. The callable can also take begin/end indices for chunk processing.