blob: 54c9a72a873c059de0815a6d1e3fae7567447b0a [file] [log] [blame]
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Creates a “fork-join” scope `s` and invokes the closure with a reference to `s`. This closure can then spawn asynchronous tasks into `s`. Those tasks may run asynchronously with respect to the closure; they may themselves spawn additional tasks into `s`. When the closure returns, it will block until all tasks that have been spawned into `s` complete."><meta name="keywords" content="rust, rustlang, rust-lang, scope"><title>scope in rayon_core - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc fn"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../rayon_core/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../rayon_core/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><div class="sidebar-elems"><h2><a href="index.html">In rayon_core</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Function <a href="index.html">rayon_core</a>::<wbr><a class="fn" href="#">scope</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/rayon_core/scope/mod.rs.html#304-313">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust fn"><code>pub fn scope&lt;'scope, OP, R&gt;(op: OP) -&gt; R<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;OP: FnOnce(&amp;<a class="struct" href="struct.Scope.html" title="struct rayon_core::Scope">Scope</a>&lt;'scope&gt;) -&gt; R + Send,<br>&nbsp;&nbsp;&nbsp;&nbsp;R: Send,</span></code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Creates a “fork-join” scope <code>s</code> and invokes the closure with a
reference to <code>s</code>. This closure can then spawn asynchronous tasks
into <code>s</code>. Those tasks may run asynchronously with respect to the
closure; they may themselves spawn additional tasks into <code>s</code>. When
the closure returns, it will block until all tasks that have been
spawned into <code>s</code> complete.</p>
<p><code>scope()</code> is a more flexible building block compared to <code>join()</code>,
since a loop can be used to spawn any number of tasks without
recursing. However, that flexibility comes at a performance price:
tasks spawned using <code>scope()</code> must be allocated onto the heap,
whereas <code>join()</code> can make exclusive use of the stack. <strong>Prefer
<code>join()</code> (or, even better, parallel iterators) where possible.</strong></p>
<h2 id="example"><a href="#example">Example</a></h2>
<p>The Rayon <code>join()</code> function launches two closures and waits for them
to stop. One could implement <code>join()</code> using a scope like so, although
it would be less efficient than the real implementation:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">pub fn </span>join&lt;A,B,RA,RB&gt;(oper_a: A, oper_b: B) -&gt; (RA, RB)
<span class="kw">where </span>A: FnOnce() -&gt; RA + Send,
B: FnOnce() -&gt; RB + Send,
RA: Send,
RB: Send,
{
<span class="kw">let </span><span class="kw-2">mut </span>result_a: <span class="prelude-ty">Option</span>&lt;RA&gt; = <span class="prelude-val">None</span>;
<span class="kw">let </span><span class="kw-2">mut </span>result_b: <span class="prelude-ty">Option</span>&lt;RB&gt; = <span class="prelude-val">None</span>;
rayon::scope(|s| {
s.spawn(|<span class="kw">_</span>| result_a = <span class="prelude-val">Some</span>(oper_a()));
s.spawn(|<span class="kw">_</span>| result_b = <span class="prelude-val">Some</span>(oper_b()));
});
(result_a.unwrap(), result_b.unwrap())
}</code></pre></div>
<h2 id="a-note-on-threading"><a href="#a-note-on-threading">A note on threading</a></h2>
<p>The closure given to <code>scope()</code> executes in the Rayon thread-pool,
as do those given to <code>spawn()</code>. This means that you can’t access
thread-local variables (well, you can, but they may have
unexpected values).</p>
<h2 id="task-execution"><a href="#task-execution">Task execution</a></h2>
<p>Task execution potentially starts as soon as <code>spawn()</code> is called.
The task will end sometime before <code>scope()</code> returns. Note that the
<em>closure</em> given to scope may return much earlier. In general
the lifetime of a scope created like <code>scope(body)</code> goes something like this:</p>
<ul>
<li>Scope begins when <code>scope(body)</code> is called</li>
<li>Scope body <code>body()</code> is invoked
<ul>
<li>Scope tasks may be spawned</li>
</ul>
</li>
<li>Scope body returns</li>
<li>Scope tasks execute, possibly spawning more tasks</li>
<li>Once all tasks are done, scope ends and <code>scope()</code> returns</li>
</ul>
<p>To see how and when tasks are joined, consider this example:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// point start
</span>rayon::scope(|s| {
s.spawn(|s| { <span class="comment">// task s.1
</span>s.spawn(|s| { <span class="comment">// task s.1.1
</span>rayon::scope(|t| {
t.spawn(|<span class="kw">_</span>| ()); <span class="comment">// task t.1
</span>t.spawn(|<span class="kw">_</span>| ()); <span class="comment">// task t.2
</span>});
});
});
s.spawn(|s| { <span class="comment">// task s.2
</span>});
<span class="comment">// point mid
</span>});
<span class="comment">// point end</span></code></pre></div>
<p>The various tasks that are run will execute roughly like so:</p>
<div class="example-wrap"><pre class="language-notrust"><code>| (start)
|
| (scope `s` created)
+-----------------------------------------------+ (task s.2)
+-------+ (task s.1) |
| | |
| +---+ (task s.1.1) |
| | | |
| | | (scope `t` created) |
| | +----------------+ (task t.2) |
| | +---+ (task t.1) | |
| (mid) | | | | |
: | + &lt;-+------------+ (scope `t` ends) |
: | | |
|&lt;------+---+-----------------------------------+ (scope `s` ends)
|
| (end)</code></pre></div>
<p>The point here is that everything spawned into scope <code>s</code> will
terminate (at latest) at the same point – right before the
original call to <code>rayon::scope</code> returns. This includes new
subtasks created by other subtasks (e.g., task <code>s.1.1</code>). If a new
scope is created (such as <code>t</code>), the things spawned into that scope
will be joined before that scope returns, which in turn occurs
before the creating task (task <code>s.1.1</code> in this case) finishes.</p>
<p>There is no guaranteed order of execution for spawns in a scope,
given that other threads may steal tasks at any time. However, they
are generally prioritized in a LIFO order on the thread from which
they were spawned. So in this example, absent any stealing, we can
expect <code>s.2</code> to execute before <code>s.1</code>, and <code>t.2</code> before <code>t.1</code>. Other
threads always steal from the other end of the deque, like FIFO
order. The idea is that “recent” tasks are most likely to be fresh
in the local CPU’s cache, while other threads can steal older
“stale” tasks. For an alternate approach, consider
<a href="fn.scope_fifo.html"><code>scope_fifo()</code></a> instead.</p>
<h2 id="accessing-stack-data"><a href="#accessing-stack-data">Accessing stack data</a></h2>
<p>In general, spawned tasks may access stack data in place that
outlives the scope itself. Other data must be fully owned by the
spawned task.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>ok: Vec&lt;i32&gt; = <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];
rayon::scope(|s| {
<span class="kw">let </span>bad: Vec&lt;i32&gt; = <span class="macro">vec!</span>[<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>];
s.spawn(|<span class="kw">_</span>| {
<span class="comment">// We can access `ok` because outlives the scope `s`.
</span><span class="macro">println!</span>(<span class="string">&quot;ok: {:?}&quot;</span>, ok);
<span class="comment">// If we just try to use `bad` here, the closure will borrow `bad`
// (because we are just printing it out, and that only requires a
// borrow), which will result in a compilation error. Read on
// for options.
// println!(&quot;bad: {:?}&quot;, bad);
</span>});
});</code></pre></div>
<p>As the comments example above suggest, to reference <code>bad</code> we must
take ownership of it. One way to do this is to detach the closure
from the surrounding stack frame, using the <code>move</code> keyword. This
will cause it to take ownership of <em>all</em> the variables it touches,
in this case including both <code>ok</code> <em>and</em> <code>bad</code>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>ok: Vec&lt;i32&gt; = <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];
rayon::scope(|s| {
<span class="kw">let </span>bad: Vec&lt;i32&gt; = <span class="macro">vec!</span>[<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>];
s.spawn(<span class="kw">move </span>|<span class="kw">_</span>| {
<span class="macro">println!</span>(<span class="string">&quot;ok: {:?}&quot;</span>, ok);
<span class="macro">println!</span>(<span class="string">&quot;bad: {:?}&quot;</span>, bad);
});
<span class="comment">// That closure is fine, but now we can&#39;t use `ok` anywhere else,
// since it is owned by the previous task:
// s.spawn(|_| println!(&quot;ok: {:?}&quot;, ok));
</span>});</code></pre></div>
<p>While this works, it could be a problem if we want to use <code>ok</code> elsewhere.
There are two choices. We can keep the closure as a <code>move</code> closure, but
instead of referencing the variable <code>ok</code>, we create a shadowed variable that
is a borrow of <code>ok</code> and capture <em>that</em>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>ok: Vec&lt;i32&gt; = <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];
rayon::scope(|s| {
<span class="kw">let </span>bad: Vec&lt;i32&gt; = <span class="macro">vec!</span>[<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>];
<span class="kw">let </span>ok: <span class="kw-2">&amp;</span>Vec&lt;i32&gt; = <span class="kw-2">&amp;</span>ok; <span class="comment">// shadow the original `ok`
</span>s.spawn(<span class="kw">move </span>|<span class="kw">_</span>| {
<span class="macro">println!</span>(<span class="string">&quot;ok: {:?}&quot;</span>, ok); <span class="comment">// captures the shadowed version
</span><span class="macro">println!</span>(<span class="string">&quot;bad: {:?}&quot;</span>, bad);
});
<span class="comment">// Now we too can use the shadowed `ok`, since `&amp;Vec&lt;i32&gt;` references
// can be shared freely. Note that we need a `move` closure here though,
// because otherwise we&#39;d be trying to borrow the shadowed `ok`,
// and that doesn&#39;t outlive `scope`.
</span>s.spawn(<span class="kw">move </span>|<span class="kw">_</span>| <span class="macro">println!</span>(<span class="string">&quot;ok: {:?}&quot;</span>, ok));
});</code></pre></div>
<p>Another option is not to use the <code>move</code> keyword but instead to take ownership
of individual variables:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>ok: Vec&lt;i32&gt; = <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];
rayon::scope(|s| {
<span class="kw">let </span>bad: Vec&lt;i32&gt; = <span class="macro">vec!</span>[<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>];
s.spawn(|<span class="kw">_</span>| {
<span class="comment">// Transfer ownership of `bad` into a local variable (also named `bad`).
// This will force the closure to take ownership of `bad` from the environment.
</span><span class="kw">let </span>bad = bad;
<span class="macro">println!</span>(<span class="string">&quot;ok: {:?}&quot;</span>, ok); <span class="comment">// `ok` is only borrowed.
</span><span class="macro">println!</span>(<span class="string">&quot;bad: {:?}&quot;</span>, bad); <span class="comment">// refers to our local variable, above.
</span>});
s.spawn(|<span class="kw">_</span>| <span class="macro">println!</span>(<span class="string">&quot;ok: {:?}&quot;</span>, ok)); <span class="comment">// we too can borrow `ok`
</span>});</code></pre></div>
<h2 id="panics"><a href="#panics">Panics</a></h2>
<p>If a panic occurs, either in the closure given to <code>scope()</code> or in
any of the spawned jobs, that panic will be propagated and the
call to <code>scope()</code> will panic. If multiple panics occurs, it is
non-deterministic which of their panic values will propagate.
Regardless, once a task is spawned using <code>scope.spawn()</code>, it will
execute, even if the spawning task should later panic. <code>scope()</code>
returns once all spawned jobs have completed, and any panics are
propagated at that point.</p>
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="rayon_core" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>