Skip to main content

scope_bounded

Function scope_bounded 

Source
pub fn scope_bounded<'env, F, R>(
    number_of_tasks: usize,
    f: F,
) -> impl Iterator<Item = R>
where R: Send + 'env, F: for<'scope> FnOnce(&'scope Scope<'scope, 'env, R>) + 'env,
Expand description

Helper method to spawn tasks in parallel, ensuring that all tasks are awaited and errors are handled. Also ensures turbo tasks and tracing context are maintained across the tasks.

Jobs are added to a shared work queue and processed by the calling thread plus up to runtime worker threads - 1 opportunistic helpers. The helpers are a pure optimization — the calling thread drains the whole queue by itself if none ever runs — so this does not deadlock on a thread-limited runtime or when the worker threads are otherwise occupied. Jobs must be independent (they must not block waiting on each other), since the degree of real concurrency is bounded by the runtime’s worker threads.

Be aware that although this function avoids starving other independently spawned tasks, any other code running concurrently in the same task will be suspended during the call to block_in_place. This can happen e.g. when using the join! macro. To avoid this issue, call scope_bounded in spawn_blocking.