<!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 `JoinHandle` struct in crate `std`."> <meta name="keywords" content="rust, rustlang, rust-lang, JoinHandle"> <title>std::thread::JoinHandle - 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="../../main.css" id="themeStyle"> <script src="../../storage.js"></script> <link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"> </head> <body class="rustdoc struct"> <!--[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='../../std/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a> <p class='location'>Struct JoinHandle</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#methods">Methods</a><div class="sidebar-links"><a href="#method.thread">thread</a><a href="#method.join">join</a></div><a class="sidebar-title" href="#implementations">Trait Implementations</a><div class="sidebar-links"><a href="#impl-Debug">Debug</a><a href="#impl-JoinHandleExt">JoinHandleExt</a><a href="#impl-AsRawHandle">AsRawHandle</a><a href="#impl-IntoRawHandle">IntoRawHandle</a></div></div><p class='location'><a href='../index.html'>std</a>::<wbr><a href='index.html'>thread</a></p><script>window.sidebarCurrent = {name: 'JoinHandle', ty: 'struct', 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"> <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"> <h1 class='fqn'><span class='in-band'>Struct <a href='../index.html'>std</a>::<wbr><a href='index.html'>thread</a>::<wbr><a class="struct" href=''>JoinHandle</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.0</span><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/std/thread/mod.rs.html#1271' title='goto source code'>[src]</a></span></h1> <pre class='rust struct'>pub struct JoinHandle<T>(_);</pre><div class='docblock'><p>An owned permission to join on a thread (block on its termination).</p> <p>A <code>JoinHandle</code> <em>detaches</em> the associated thread when it is dropped, which means that there is no longer any handle to thread and no way to <code>join</code> on it.</p> <p>Due to platform restrictions, it is not possible to <a href="../../std/clone/trait.Clone.html"><code>Clone</code></a> this handle: the ability to join a thread is a uniquely-owned permission.</p> <p>This <code>struct</code> is created by the <a href="fn.spawn.html"><code>thread::spawn</code></a> function and the <a href="struct.Builder.html#method.spawn"><code>thread::Builder::spawn</code></a> method.</p> <h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1> <p>Creation from <a href="fn.spawn.html"><code>thread::spawn</code></a>:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>; <span class="kw">let</span> <span class="ident">join_handle</span>: <span class="ident">thread</span>::<span class="ident">JoinHandle</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="op">||</span> { <span class="comment">// some work here</span> });<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20join_handle%3A%20thread%3A%3AJoinHandle%3C_%3E%20%3D%20thread%3A%3Aspawn(%7C%7C%20%7B%0A%20%20%20%20%2F%2F%20some%20work%20here%0A%7D)%3B%0A%7D">Run</a></pre> <p>Creation from <a href="struct.Builder.html#method.spawn"><code>thread::Builder::spawn</code></a>:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>; <span class="kw">let</span> <span class="ident">builder</span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">Builder</span>::<span class="ident">new</span>(); <span class="kw">let</span> <span class="ident">join_handle</span>: <span class="ident">thread</span>::<span class="ident">JoinHandle</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">spawn</span>(<span class="op">||</span> { <span class="comment">// some work here</span> }).<span class="ident">unwrap</span>();<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20builder%20%3D%20thread%3A%3ABuilder%3A%3Anew()%3B%0A%0Alet%20join_handle%3A%20thread%3A%3AJoinHandle%3C_%3E%20%3D%20builder.spawn(%7C%7C%20%7B%0A%20%20%20%20%2F%2F%20some%20work%20here%0A%7D).unwrap()%3B%0A%7D">Run</a></pre> <p>Child being detached and outliving its parent:</p> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>; <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">time</span>::<span class="ident">Duration</span>; <span class="kw">let</span> <span class="ident">original_thread</span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="op">||</span> { <span class="kw">let</span> <span class="ident">_detached_thread</span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="op">||</span> { <span class="comment">// Here we sleep to make sure that the first thread returns before.</span> <span class="ident">thread</span>::<span class="ident">sleep</span>(<span class="ident">Duration</span>::<span class="ident">from_millis</span>(<span class="number">10</span>)); <span class="comment">// This will be called, even though the JoinHandle is dropped.</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">"♫ Still alive ♫"</span>); }); }); <span class="ident">original_thread</span>.<span class="ident">join</span>().<span class="ident">expect</span>(<span class="string">"The thread being joined has panicked"</span>); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Original thread is joined."</span>); <span class="comment">// We make sure that the new thread has time to run, before the main</span> <span class="comment">// thread returns.</span> <span class="ident">thread</span>::<span class="ident">sleep</span>(<span class="ident">Duration</span>::<span class="ident">from_millis</span>(<span class="number">1000</span>));<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Athread%3B%0Ause%20std%3A%3Atime%3A%3ADuration%3B%0A%0Alet%20original_thread%20%3D%20thread%3A%3Aspawn(%7C%7C%20%7B%0A%20%20%20%20let%20_detached_thread%20%3D%20thread%3A%3Aspawn(%7C%7C%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Here%20we%20sleep%20to%20make%20sure%20that%20the%20first%20thread%20returns%20before.%0A%20%20%20%20%20%20%20%20thread%3A%3Asleep(Duration%3A%3Afrom_millis(10))%3B%0A%20%20%20%20%20%20%20%20%2F%2F%20This%20will%20be%20called%2C%20even%20though%20the%20JoinHandle%20is%20dropped.%0A%20%20%20%20%20%20%20%20println!(%22%E2%99%AB%20Still%20alive%20%E2%99%AB%22)%3B%0A%20%20%20%20%7D)%3B%0A%7D)%3B%0A%0Aoriginal_thread.join().expect(%22The%20thread%20being%20joined%20has%20panicked%22)%3B%0Aprintln!(%22Original%20thread%20is%20joined.%22)%3B%0A%0A%2F%2F%20We%20make%20sure%20that%20the%20new%20thread%20has%20time%20to%20run%2C%20before%20the%20main%0A%2F%2F%20thread%20returns.%0A%0Athread%3A%3Asleep(Duration%3A%3Afrom_millis(1000))%3B%0A%7D">Run</a></pre> </div> <h2 id='methods' class='small-section-header'> Methods<a href='#methods' class='anchor'></a> </h2> <h3 id='impl' class='impl'><span class='in-band'><code>impl<T> <a class="struct" href="../../std/thread/struct.JoinHandle.html" title="struct std::thread::JoinHandle">JoinHandle</a><T></code><a href='#impl' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/thread/mod.rs.html#1273-1324' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.thread' class="method"><span id='thread.v' class='invisible'><code>pub fn <a href='#method.thread' class='fnname'>thread</a>(&self) -> &<a class="struct" href="../../std/thread/struct.Thread.html" title="struct std::thread::Thread">Thread</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/thread/mod.rs.html#1291-1293' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Extracts a handle to the underlying thread.</p> <h1 id="examples-1" class="section-header"><a href="#examples-1">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>; <span class="kw">let</span> <span class="ident">builder</span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">Builder</span>::<span class="ident">new</span>(); <span class="kw">let</span> <span class="ident">join_handle</span>: <span class="ident">thread</span>::<span class="ident">JoinHandle</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">spawn</span>(<span class="op">||</span> { <span class="comment">// some work here</span> }).<span class="ident">unwrap</span>(); <span class="kw">let</span> <span class="ident">thread</span> <span class="op">=</span> <span class="ident">join_handle</span>.<span class="ident">thread</span>(); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"thread id: {:?}"</span>, <span class="ident">thread</span>.<span class="ident">id</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20builder%20%3D%20thread%3A%3ABuilder%3A%3Anew()%3B%0A%0Alet%20join_handle%3A%20thread%3A%3AJoinHandle%3C_%3E%20%3D%20builder.spawn(%7C%7C%20%7B%0A%20%20%20%20%2F%2F%20some%20work%20here%0A%7D).unwrap()%3B%0A%0Alet%20thread%20%3D%20join_handle.thread()%3B%0Aprintln!(%22thread%20id%3A%20%7B%3A%3F%7D%22%2C%20thread.id())%3B%0A%7D">Run</a></pre> </div><h4 id='method.join' class="method"><span id='join.v' class='invisible'><code>pub fn <a href='#method.join' class='fnname'>join</a>(self) -> <a class="type" href="../../std/thread/type.Result.html" title="type std::thread::Result">Result</a><T></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/thread/mod.rs.html#1321-1323' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Waits for the associated thread to finish.</p> <p>If the child thread panics, <a href="../../std/result/enum.Result.html#variant.Err"><code>Err</code></a> is returned with the parameter given to <a href="../../std/macro.panic.html"><code>panic</code></a>.</p> <h1 id="panics" class="section-header"><a href="#panics">Panics</a></h1> <p>This function may panic on some platforms if a thread attempts to join itself or otherwise may create a deadlock with joining threads.</p> <h1 id="examples-2" class="section-header"><a href="#examples-2">Examples</a></h1> <pre class="rust rust-example-rendered"> <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>; <span class="kw">let</span> <span class="ident">builder</span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">Builder</span>::<span class="ident">new</span>(); <span class="kw">let</span> <span class="ident">join_handle</span>: <span class="ident">thread</span>::<span class="ident">JoinHandle</span><span class="op"><</span>_<span class="op">></span> <span class="op">=</span> <span class="ident">builder</span>.<span class="ident">spawn</span>(<span class="op">||</span> { <span class="comment">// some work here</span> }).<span class="ident">unwrap</span>(); <span class="ident">join_handle</span>.<span class="ident">join</span>().<span class="ident">expect</span>(<span class="string">"Couldn't join on the associated thread"</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20builder%20%3D%20thread%3A%3ABuilder%3A%3Anew()%3B%0A%0Alet%20join_handle%3A%20thread%3A%3AJoinHandle%3C_%3E%20%3D%20builder.spawn(%7C%7C%20%7B%0A%20%20%20%20%2F%2F%20some%20work%20here%0A%7D).unwrap()%3B%0Ajoin_handle.join().expect(%22Couldn't%20join%20on%20the%20associated%20thread%22)%3B%0A%7D">Run</a></pre> </div></div> <h2 id='implementations' class='small-section-header'> Trait Implementations<a href='#implementations' class='anchor'></a> </h2> <h3 id='impl-Debug' class='impl'><span class='in-band'><code>impl<T> <a class="trait" href="../../std/fmt/trait.Debug.html" title="trait std::fmt::Debug">Debug</a> for <a class="struct" href="../../std/thread/struct.JoinHandle.html" title="struct std::thread::JoinHandle">JoinHandle</a><T></code><a href='#impl-Debug' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.16.0'>1.16.0</div><a class='srclink' href='../../src/std/thread/mod.rs.html#1335-1339' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.fmt' class="method"><span id='fmt.v' class='invisible'><code>fn <a href='../../std/fmt/trait.Debug.html#tymethod.fmt' class='fnname'>fmt</a>(&self, f: &mut <a class="struct" href="../../std/fmt/struct.Formatter.html" title="struct std::fmt::Formatter">Formatter</a>) -> <a class="type" href="../../std/fmt/type.Result.html" title="type std::fmt::Result">Result</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/thread/mod.rs.html#1336-1338' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Formats the value using the given formatter. <a href="../../std/fmt/trait.Debug.html#tymethod.fmt">Read more</a></p> </div></div><h3 id='impl-JoinHandleExt' class='impl'><span class='in-band'><code>impl<T> <a class="trait" href="../../std/os/unix/thread/trait.JoinHandleExt.html" title="trait std::os::unix::thread::JoinHandleExt">JoinHandleExt</a> for <a class="struct" href="../../std/thread/struct.JoinHandle.html" title="struct std::thread::JoinHandle">JoinHandle</a><T></code><a href='#impl-JoinHandleExt' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.9.0'>1.9.0</div><a class='srclink' href='../../src/std/sys/unix/ext/thread.rs.html#41-49' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.as_pthread_t' class="method"><span id='as_pthread_t.v' class='invisible'><code>fn <a href='../../std/os/unix/thread/trait.JoinHandleExt.html#tymethod.as_pthread_t' class='fnname'>as_pthread_t</a>(&self) -> <a class="type" href="../../std/os/unix/thread/type.RawPthread.html" title="type std::os::unix::thread::RawPthread">RawPthread</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sys/unix/ext/thread.rs.html#42-44' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab portability'>This is supported on <strong>Unix</strong> only.</div></div><div class='docblock'><p>Extracts the raw pthread_t without taking ownership</p> </div><h4 id='method.into_pthread_t' class="method"><span id='into_pthread_t.v' class='invisible'><code>fn <a href='../../std/os/unix/thread/trait.JoinHandleExt.html#tymethod.into_pthread_t' class='fnname'>into_pthread_t</a>(self) -> <a class="type" href="../../std/os/unix/thread/type.RawPthread.html" title="type std::os::unix::thread::RawPthread">RawPthread</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sys/unix/ext/thread.rs.html#46-48' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab portability'>This is supported on <strong>Unix</strong> only.</div></div><div class='docblock'><p>Consumes the thread, returning the raw pthread_t <a href="../../std/os/unix/thread/trait.JoinHandleExt.html#tymethod.into_pthread_t">Read more</a></p> </div></div><h3 id='impl-AsRawHandle' class='impl'><span class='in-band'><code>impl<T> <a class="trait" href="../../std/os/windows/io/trait.AsRawHandle.html" title="trait std::os::windows::io::AsRawHandle">AsRawHandle</a> for <a class="struct" href="../../std/thread/struct.JoinHandle.html" title="struct std::thread::JoinHandle">JoinHandle</a><T></code><a href='#impl-AsRawHandle' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.9.0'>1.9.0</div><a class='srclink' href='../../src/std/sys/windows/ext/thread.rs.html#20-24' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.as_raw_handle' class="method"><span id='as_raw_handle.v' class='invisible'><code>fn <a href='../../std/os/windows/io/trait.AsRawHandle.html#tymethod.as_raw_handle' class='fnname'>as_raw_handle</a>(&self) -> <a class="type" href="../../std/os/windows/io/type.RawHandle.html" title="type std::os::windows::io::RawHandle">RawHandle</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sys/windows/ext/thread.rs.html#21-23' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab portability'>This is supported on <strong>Windows</strong> only.</div></div><div class='docblock'><p>Extracts the raw handle, without taking any ownership.</p> </div></div><h3 id='impl-IntoRawHandle' class='impl'><span class='in-band'><code>impl<T> <a class="trait" href="../../std/os/windows/io/trait.IntoRawHandle.html" title="trait std::os::windows::io::IntoRawHandle">IntoRawHandle</a> for <a class="struct" href="../../std/thread/struct.JoinHandle.html" title="struct std::thread::JoinHandle">JoinHandle</a><T></code><a href='#impl-IntoRawHandle' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.9.0'>1.9.0</div><a class='srclink' href='../../src/std/sys/windows/ext/thread.rs.html#27-31' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.into_raw_handle' class="method"><span id='into_raw_handle.v' class='invisible'><code>fn <a href='../../std/os/windows/io/trait.IntoRawHandle.html#tymethod.into_raw_handle' class='fnname'>into_raw_handle</a>(self) -> <a class="type" href="../../std/os/windows/io/type.RawHandle.html" title="type std::os::windows::io::RawHandle">RawHandle</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sys/windows/ext/thread.rs.html#28-30' title='goto source code'>[src]</a></span></h4> <div class='stability'><div class='stab portability'>This is supported on <strong>Windows</strong> only.</div></div><div class='docblock'><p>Consumes this object, returning the raw underlying handle. <a href="../../std/os/windows/io/trait.IntoRawHandle.html#tymethod.into_raw_handle">Read more</a></p> </div></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><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> </div> </div> </aside> <script> window.rootPath = "../../"; window.currentCrate = "std"; </script> <script src="../../main.js"></script> <script defer src="../../search-index.js"></script> </body> </html>