227 lines
No EOL
12 KiB
HTML
227 lines
No EOL
12 KiB
HTML
<!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 `Scope` struct in crate `crossbeam`.">
|
||
<meta name="keywords" content="rust, rustlang, rust-lang, Scope">
|
||
|
||
<title>crossbeam::Scope - Rust</title>
|
||
|
||
<link rel="stylesheet" type="text/css" href="../rustdoc.css">
|
||
<link rel="stylesheet" type="text/css" href="../main.css">
|
||
|
||
|
||
|
||
</head>
|
||
<body class="rustdoc">
|
||
<!--[if lte IE 8]>
|
||
<div class="warning">
|
||
This old browser is unsupported and will most likely display funky
|
||
things.
|
||
</div>
|
||
<![endif]-->
|
||
|
||
|
||
|
||
<nav class="sidebar">
|
||
|
||
<p class='location'><a href='index.html'>crossbeam</a></p><script>window.sidebarCurrent = {name: 'Scope', ty: 'struct', relpath: ''};</script><script defer src="sidebar-items.js"></script>
|
||
</nav>
|
||
|
||
<nav class="sub">
|
||
<form class="search-form js-only">
|
||
<div class="search-container">
|
||
<input class="search-input" name="search"
|
||
autocomplete="off"
|
||
placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
|
||
type="search">
|
||
</div>
|
||
</form>
|
||
</nav>
|
||
|
||
<section id='main' class="content struct">
|
||
<h1 class='fqn'><span class='in-band'>Struct <a href='index.html'>crossbeam</a>::<wbr><a class='struct' href=''>Scope</a></span><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 id='src-857' class='srclink' href='../src/crossbeam/scoped.rs.html.html#11-13' title='goto source code'>[src]</a></span></h1>
|
||
<pre class='rust struct'>pub struct Scope<'a> {
|
||
// some fields omitted
|
||
}</pre><h2 id='methods'>Methods</h2><h3 class='impl'><code>impl<'a> <a class='struct' href='../crossbeam/struct.Scope.html' title='crossbeam::Scope'>Scope</a><'a></code></h3><div class='impl-items'><h4 id='method.defer' class='method'><code>fn <a href='#method.defer' class='fnname'>defer</a><F>(&self, f: F) <span class='where'>where F: <a class='trait' href='https://doc.rust-lang.org/nightly/core/ops/trait.FnOnce.html' title='core::ops::FnOnce'>FnOnce</a>() + 'a</span></code></h4>
|
||
<div class='docblock'><p>Schedule code to be executed when exiting the scope.</p>
|
||
|
||
<p>This is akin to having a destructor on the stack, except that it is
|
||
<em>guaranteed</em> to be run.</p>
|
||
</div><h4 id='method.spawn' class='method'><code>fn <a href='#method.spawn' class='fnname'>spawn</a><F, T>(&self, f: F) -> <a class='struct' href='../crossbeam/struct.ScopedJoinHandle.html' title='crossbeam::ScopedJoinHandle'>ScopedJoinHandle</a><T> <span class='where'>where F: <a class='trait' href='https://doc.rust-lang.org/nightly/core/ops/trait.FnOnce.html' title='core::ops::FnOnce'>FnOnce</a>() -> T + <a class='trait' href='https://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + 'a, T: <a class='trait' href='https://doc.rust-lang.org/nightly/core/marker/trait.Send.html' title='core::marker::Send'>Send</a> + 'a</span></code></h4>
|
||
<div class='docblock'><p>Create a scoped thread.</p>
|
||
|
||
<p><code>spawn</code> is similar to the <a href="http://doc.rust-lang.org/std/thread/fn.spawn.html"><code>spawn</code></a> function in Rust's standard library. The
|
||
difference is that this thread is scoped, meaning that it's guaranteed to terminate
|
||
before the current stack frame goes away, allowing you to reference the parent stack frame
|
||
directly. This is ensured by having the parent thread join on the child thread before the
|
||
scope exits.</p>
|
||
|
||
<h1 id='examples' class='section-header'><a href='#examples'>Examples</a></h1>
|
||
<p>A basic scoped thread:</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='ident'>crossbeam</span>::<span class='ident'>scope</span>(<span class='op'>|</span><span class='ident'>scope</span><span class='op'>|</span> {
|
||
<span class='ident'>scope</span>.<span class='ident'>spawn</span>(<span class='op'>||</span> {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Hello from a scoped thread!"</span>);
|
||
});
|
||
});</pre>
|
||
|
||
<p>When writing concurrent Rust programs, you'll sometimes see a pattern like this, using
|
||
<a href="http://doc.rust-lang.org/std/thread/fn.spawn.html"><code>std::thread::spawn</code></a>:</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='kw'>let</span> <span class='ident'>array</span> <span class='op'>=</span> [<span class='number'>1</span>, <span class='number'>2</span>, <span class='number'>3</span>];
|
||
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>guards</span> <span class='op'>=</span> <span class='macro'>vec</span><span class='macro'>!</span>[];
|
||
|
||
<span class='kw'>for</span> <span class='ident'>i</span> <span class='kw'>in</span> <span class='kw-2'>&</span><span class='ident'>array</span> {
|
||
<span class='kw'>let</span> <span class='ident'>guard</span> <span class='op'>=</span> <span class='ident'>std</span>::<span class='ident'>thread</span>::<span class='ident'>spawn</span>(<span class='kw'>move</span> <span class='op'>||</span> {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"element: {}"</span>, <span class='ident'>i</span>);
|
||
});
|
||
|
||
<span class='ident'>guards</span>.<span class='ident'>push</span>(<span class='ident'>guard</span>);
|
||
}
|
||
|
||
<span class='kw'>for</span> <span class='ident'>guard</span> <span class='kw'>in</span> <span class='ident'>guards</span> {
|
||
<span class='ident'>guard</span>.<span class='ident'>join</span>().<span class='ident'>unwrap</span>();
|
||
}</pre>
|
||
|
||
<p>The basic pattern is:</p>
|
||
|
||
<ol>
|
||
<li>Iterate over some collection.</li>
|
||
<li>Spin up a thread to operate on each part of the collection.</li>
|
||
<li>Join all the threads.</li>
|
||
</ol>
|
||
|
||
<p>However, this code actually gives an error:</p>
|
||
|
||
<pre><code class="language-text">error: `array` does not live long enough
|
||
for i in &array {
|
||
^~~~~
|
||
in expansion of for loop expansion
|
||
note: expansion site
|
||
note: reference must be valid for the static lifetime...
|
||
note: ...but borrowed value is only valid for the block suffix following statement 0 at ...
|
||
let array = [1, 2, 3];
|
||
let mut guards = vec![];
|
||
|
||
for i in &array {
|
||
let guard = std::thread::spawn(move || {
|
||
println!("element: {}", i);
|
||
...
|
||
error: aborting due to previous error
|
||
</code></pre>
|
||
|
||
<p>Because <a href="http://doc.rust-lang.org/std/thread/fn.spawn.html"><code>std::thread::spawn</code></a> doesn't know about this scope, it requires a
|
||
<code>'static</code> lifetime. One way of giving it a proper lifetime is to use an <a href="http://doc.rust-lang.org/stable/std/sync/struct.Arc.html"><code>Arc</code></a>:</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>sync</span>::<span class='ident'>Arc</span>;
|
||
|
||
<span class='kw'>let</span> <span class='ident'>array</span> <span class='op'>=</span> <span class='ident'>Arc</span>::<span class='ident'>new</span>([<span class='number'>1</span>, <span class='number'>2</span>, <span class='number'>3</span>]);
|
||
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>guards</span> <span class='op'>=</span> <span class='macro'>vec</span><span class='macro'>!</span>[];
|
||
|
||
<span class='kw'>for</span> <span class='ident'>i</span> <span class='kw'>in</span> (<span class='number'>0</span>..<span class='ident'>array</span>.<span class='ident'>len</span>()) {
|
||
<span class='kw'>let</span> <span class='ident'>a</span> <span class='op'>=</span> <span class='ident'>array</span>.<span class='ident'>clone</span>();
|
||
|
||
<span class='kw'>let</span> <span class='ident'>guard</span> <span class='op'>=</span> <span class='ident'>std</span>::<span class='ident'>thread</span>::<span class='ident'>spawn</span>(<span class='kw'>move</span> <span class='op'>||</span> {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"element: {}"</span>, <span class='ident'>a</span>[<span class='ident'>i</span>]);
|
||
});
|
||
|
||
<span class='ident'>guards</span>.<span class='ident'>push</span>(<span class='ident'>guard</span>);
|
||
}
|
||
|
||
<span class='kw'>for</span> <span class='ident'>guard</span> <span class='kw'>in</span> <span class='ident'>guards</span> {
|
||
<span class='ident'>guard</span>.<span class='ident'>join</span>().<span class='ident'>unwrap</span>();
|
||
}</pre>
|
||
|
||
<p>But this introduces unnecessary allocation, as <code>Arc<T></code> puts its data on the heap, and we
|
||
also end up dealing with reference counts. We know that we're joining the threads before
|
||
our function returns, so just taking a reference <em>should</em> be safe. Rust can't know that,
|
||
though.</p>
|
||
|
||
<p>Enter scoped threads. Here's our original example, using <code>spawn</code> from crossbeam rather
|
||
than from <code>std::thread</code>:</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='kw'>let</span> <span class='ident'>array</span> <span class='op'>=</span> [<span class='number'>1</span>, <span class='number'>2</span>, <span class='number'>3</span>];
|
||
|
||
<span class='ident'>crossbeam</span>::<span class='ident'>scope</span>(<span class='op'>|</span><span class='ident'>scope</span><span class='op'>|</span> {
|
||
<span class='kw'>for</span> <span class='ident'>i</span> <span class='kw'>in</span> <span class='kw-2'>&</span><span class='ident'>array</span> {
|
||
<span class='ident'>scope</span>.<span class='ident'>spawn</span>(<span class='kw'>move</span> <span class='op'>||</span> {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"element: {}"</span>, <span class='ident'>i</span>);
|
||
});
|
||
}
|
||
});</pre>
|
||
|
||
<p>Much more straightforward.</p>
|
||
</div></div><h2 id='implementations'>Trait Implementations</h2><h3 class='impl'><code>impl<'a> <a class='trait' href='https://doc.rust-lang.org/nightly/core/ops/trait.Drop.html' title='core::ops::Drop'>Drop</a> for <a class='struct' href='../crossbeam/struct.Scope.html' title='crossbeam::Scope'>Scope</a><'a></code></h3><div class='impl-items'><h4 id='method.drop' class='method'><code>fn <a href='https://doc.rust-lang.org/nightly/core/ops/trait.Drop.html#method.drop' class='fnname'>drop</a>(&mut self)</code></h4>
|
||
</div></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>?</dt>
|
||
<dd>Show this help dialog</dd>
|
||
<dt>S</dt>
|
||
<dd>Focus the search field</dd>
|
||
<dt>⇤</dt>
|
||
<dd>Move up in search results</dd>
|
||
<dt>⇥</dt>
|
||
<dd>Move down in search results</dd>
|
||
<dt>⏎</dt>
|
||
<dd>Go to active search result</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>
|
||
</div>
|
||
</div>
|
||
</aside>
|
||
|
||
|
||
|
||
<script>
|
||
window.rootPath = "../";
|
||
window.currentCrate = "crossbeam";
|
||
window.playgroundUrl = "";
|
||
</script>
|
||
<script src="../jquery.js"></script>
|
||
<script src="../main.js"></script>
|
||
|
||
<script defer src="../search-index.js"></script>
|
||
</body>
|
||
</html> |