|
|
<!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="API documentation for the Rust `thread` mod in crate `crossbeam_utils`."><meta name="keywords" content="rust, rustlang, rust-lang, thread"><title>crossbeam_utils::thread - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="shortcut icon" href="../../favicon.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../crossbeam_utils/index.html'><div class='logo-container'><img src='../../rust-logo.png' alt='logo'></div></a><p class='location'>Module thread</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'><a href='../index.html'>crossbeam_utils</a></p><script>window.sidebarCurrent = {name: 'thread', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../../src/crossbeam_utils/thread.rs.html#1-529' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../index.html'>crossbeam_utils</a>::<wbr><a class="mod" href=''>thread</a></span></h1><div class='docblock'><p>Threads that can borrow variables from the stack.</p>
|
|
|
<p>Create a scope when spawned threads need to access variables on the stack:</p>
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_utils</span>::<span class="ident">thread</span>;
|
|
|
|
|
|
<span class="kw">let</span> <span class="ident">people</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[
|
|
|
<span class="string">"Alice"</span>.<span class="ident">to_string</span>(),
|
|
|
<span class="string">"Bob"</span>.<span class="ident">to_string</span>(),
|
|
|
<span class="string">"Carol"</span>.<span class="ident">to_string</span>(),
|
|
|
];
|
|
|
|
|
|
<span class="ident">thread</span>::<span class="ident">scope</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> {
|
|
|
<span class="kw">for</span> <span class="ident">person</span> <span class="kw">in</span> <span class="kw-2">&</span><span class="ident">people</span> {
|
|
|
<span class="ident">s</span>.<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">|</span><span class="kw">_</span><span class="op">|</span> {
|
|
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello, {}!"</span>, <span class="ident">person</span>);
|
|
|
});
|
|
|
}
|
|
|
}).<span class="ident">unwrap</span>();</pre></div>
|
|
|
<h1 id="why-scoped-threads" class="section-header"><a href="#why-scoped-threads">Why scoped threads?</a></h1>
|
|
|
<p>Suppose we wanted to re-write the previous example using plain threads:</p>
|
|
|
|
|
|
<div class='information'><div class='tooltip ignore'>ⓘ<span class='tooltiptext'>This example is not tested</span></div></div><div class="example-wrap"><pre class="rust rust-example-rendered ignore">
|
|
|
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;
|
|
|
|
|
|
<span class="kw">let</span> <span class="ident">people</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[
|
|
|
<span class="string">"Alice"</span>.<span class="ident">to_string</span>(),
|
|
|
<span class="string">"Bob"</span>.<span class="ident">to_string</span>(),
|
|
|
<span class="string">"Carol"</span>.<span class="ident">to_string</span>(),
|
|
|
];
|
|
|
|
|
|
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">threads</span> <span class="op">=</span> <span class="ident">Vec</span>::<span class="ident">new</span>();
|
|
|
|
|
|
<span class="kw">for</span> <span class="ident">person</span> <span class="kw">in</span> <span class="kw-2">&</span><span class="ident">people</span> {
|
|
|
<span class="ident">threads</span>.<span class="ident">push</span>(<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">|</span><span class="kw">_</span><span class="op">|</span> {
|
|
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello, {}!"</span>, <span class="ident">person</span>);
|
|
|
}));
|
|
|
}
|
|
|
|
|
|
<span class="kw">for</span> <span class="ident">thread</span> <span class="kw">in</span> <span class="ident">threads</span> {
|
|
|
<span class="ident">thread</span>.<span class="ident">join</span>().<span class="ident">unwrap</span>();
|
|
|
}</pre></div>
|
|
|
<p>This doesn't work because the borrow checker complains about <code>people</code> not living long enough:</p>
|
|
|
<pre><code class="language-text">error[E0597]: `people` does not live long enough
|
|
|
--> src/main.rs:12:20
|
|
|
|
|
|
|
12 | for person in &people {
|
|
|
| ^^^^^^ borrowed value does not live long enough
|
|
|
...
|
|
|
21 | }
|
|
|
| - borrowed value only lives until here
|
|
|
|
|
|
|
= note: borrowed value must be valid for the static lifetime...
|
|
|
</code></pre>
|
|
|
<p>The problem here is that spawned threads are not allowed to borrow variables on stack because
|
|
|
the compiler cannot prove they will be joined before <code>people</code> is destroyed.</p>
|
|
|
<p>Scoped threads are a mechanism to guarantee to the compiler that spawned threads will be joined
|
|
|
before the scope ends.</p>
|
|
|
<h1 id="how-scoped-threads-work" class="section-header"><a href="#how-scoped-threads-work">How scoped threads work</a></h1>
|
|
|
<p>If a variable is borrowed by a thread, the thread must complete before the variable is
|
|
|
destroyed. Threads spawned using <a href="https://doc.rust-lang.org/std/thread/fn.spawn.html"><code>std::thread::spawn</code></a> can only borrow variables with the
|
|
|
<code>'static</code> lifetime because the borrow checker cannot be sure when the thread will complete.</p>
|
|
|
<p>A scope creates a clear boundary between variables outside the scope and threads inside the
|
|
|
scope. Whenever a s.spawns a thread, it promises to join the thread before the scope ends.
|
|
|
This way we guarantee to the borrow checker that scoped threads only live within the scope and
|
|
|
can safely access variables outside it.</p>
|
|
|
<h1 id="nesting-scoped-threads" class="section-header"><a href="#nesting-scoped-threads">Nesting scoped threads</a></h1>
|
|
|
<p>Sometimes scoped threads need to spawn more threads within the same scope. This is a little
|
|
|
tricky because argument <code>s</code> lives <em>inside</em> the invocation of <code>thread::scope()</code> and as such
|
|
|
cannot be borrowed by scoped threads:</p>
|
|
|
|
|
|
<div class='information'><div class='tooltip ignore'>ⓘ<span class='tooltiptext'>This example is not tested</span></div></div><div class="example-wrap"><pre class="rust rust-example-rendered ignore">
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_utils</span>::<span class="ident">thread</span>;
|
|
|
|
|
|
<span class="ident">thread</span>::<span class="ident">scope</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> {
|
|
|
<span class="ident">s</span>.<span class="ident">spawn</span>(<span class="op">|</span><span class="kw">_</span><span class="op">|</span> {
|
|
|
<span class="comment">// Not going to compile because we're trying to borrow `s`,</span>
|
|
|
<span class="comment">// which lives *inside* the scope! :(</span>
|
|
|
<span class="ident">s</span>.<span class="ident">spawn</span>(<span class="op">|</span><span class="kw">_</span><span class="op">|</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"nested thread"</span>));
|
|
|
}});
|
|
|
});</pre></div>
|
|
|
<p>Fortunately, there is a solution. Every scoped thread is passed a reference to its scope as an
|
|
|
argument, which can be used for spawning nested threads:</p>
|
|
|
|
|
|
<div class='information'><div class='tooltip ignore'>ⓘ<span class='tooltiptext'>This example is not tested</span></div></div><div class="example-wrap"><pre class="rust rust-example-rendered ignore">
|
|
|
<span class="kw">use</span> <span class="ident">crossbeam_utils</span>::<span class="ident">thread</span>;
|
|
|
|
|
|
<span class="ident">thread</span>::<span class="ident">scope</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> {
|
|
|
<span class="comment">// Note the `|s|` here.</span>
|
|
|
<span class="ident">s</span>.<span class="ident">spawn</span>(<span class="op">|</span><span class="ident">s</span><span class="op">|</span> {
|
|
|
<span class="comment">// Yay, this works because we're using a fresh argument `s`! :)</span>
|
|
|
<span class="ident">s</span>.<span class="ident">spawn</span>(<span class="op">|</span><span class="kw">_</span><span class="op">|</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"nested thread"</span>));
|
|
|
}});
|
|
|
});</pre></div>
|
|
|
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
|
|
|
<table><tr class='module-item'><td><a class="struct" href="struct.Scope.html" title='crossbeam_utils::thread::Scope struct'>Scope</a></td><td class='docblock-short'><p>A scope for spawning threads.</p>
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.ScopedJoinHandle.html" title='crossbeam_utils::thread::ScopedJoinHandle struct'>ScopedJoinHandle</a></td><td class='docblock-short'><p>A handle that can be used to join its scoped thread.</p>
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.ScopedThreadBuilder.html" title='crossbeam_utils::thread::ScopedThreadBuilder struct'>ScopedThreadBuilder</a></td><td class='docblock-short'><p>Configures the properties of a new thread.</p>
|
|
|
</td></tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
|
|
|
<table><tr class='module-item'><td><a class="fn" href="fn.scope.html" title='crossbeam_utils::thread::scope fn'>scope</a></td><td class='docblock-short'><p>Creates a new scope for spawning threads.</p>
|
|
|
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "crossbeam_utils";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html> |