<!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 `Barrier` struct in crate `std`."> <meta name="keywords" content="rust, rustlang, rust-lang, Barrier"> <title>std::sync::Barrier - 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 Barrier</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#methods">Methods</a><div class="sidebar-links"><a href="#method.new">new</a><a href="#method.wait">wait</a></div><a class="sidebar-title" href="#implementations">Trait Implementations</a><div class="sidebar-links"><a href="#impl-Debug">Debug</a></div></div><p class='location'><a href='../index.html'>std</a>::<wbr><a href='index.html'>sync</a></p><script>window.sidebarCurrent = {name: 'Barrier', 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'>sync</a>::<wbr><a class="struct" href=''>Barrier</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/sync/barrier.rs.html#41-45' title='goto source code'>[src]</a></span></h1> <pre class='rust struct'>pub struct Barrier { /* fields omitted */ }</pre><div class='docblock'><p>A barrier enables multiple threads to synchronize the beginning of some computation.</p> <h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1> <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="ident">Barrier</span>}; <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">handles</span> <span class="op">=</span> <span class="ident">Vec</span>::<span class="ident">with_capacity</span>(<span class="number">10</span>); <span class="kw">let</span> <span class="ident">barrier</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>(<span class="ident">Barrier</span>::<span class="ident">new</span>(<span class="number">10</span>)); <span class="kw">for</span> _ <span class="kw">in</span> <span class="number">0</span>..<span class="number">10</span> { <span class="kw">let</span> <span class="ident">c</span> <span class="op">=</span> <span class="ident">barrier</span>.<span class="ident">clone</span>(); <span class="comment">// The same messages will be printed together.</span> <span class="comment">// You will NOT see any interleaving.</span> <span class="ident">handles</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="macro">println</span><span class="macro">!</span>(<span class="string">"before wait"</span>); <span class="ident">c</span>.<span class="ident">wait</span>(); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"after wait"</span>); })); } <span class="comment">// Wait for other threads to finish.</span> <span class="kw">for</span> <span class="ident">handle</span> <span class="kw">in</span> <span class="ident">handles</span> { <span class="ident">handle</span>.<span class="ident">join</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%3Async%3A%3A%7BArc%2C%20Barrier%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20mut%20handles%20%3D%20Vec%3A%3Awith_capacity(10)%3B%0Alet%20barrier%20%3D%20Arc%3A%3Anew(Barrier%3A%3Anew(10))%3B%0Afor%20_%20in%200..10%20%7B%0A%20%20%20%20let%20c%20%3D%20barrier.clone()%3B%0A%20%20%20%20%2F%2F%20The%20same%20messages%20will%20be%20printed%20together.%0A%20%20%20%20%2F%2F%20You%20will%20NOT%20see%20any%20interleaving.%0A%20%20%20%20handles.push(thread%3A%3Aspawn(move%7C%7C%20%7B%0A%20%20%20%20%20%20%20%20println!(%22before%20wait%22)%3B%0A%20%20%20%20%20%20%20%20c.wait()%3B%0A%20%20%20%20%20%20%20%20println!(%22after%20wait%22)%3B%0A%20%20%20%20%7D))%3B%0A%7D%0A%2F%2F%20Wait%20for%20other%20threads%20to%20finish.%0Afor%20handle%20in%20handles%20%7B%0A%20%20%20%20handle.join().unwrap()%3B%0A%7D%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 <a class="struct" href="../../std/sync/struct.Barrier.html" title="struct std::sync::Barrier">Barrier</a></code><a href='#impl' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/barrier.rs.html#77-160' title='goto source code'>[src]</a></span></h3> <div class='impl-items'><h4 id='method.new' class="method"><span id='new.v' class='invisible'><code>pub fn <a href='#method.new' class='fnname'>new</a>(n: <a class="primitive" href="../primitive.usize.html">usize</a>) -> <a class="struct" href="../../std/sync/struct.Barrier.html" title="struct std::sync::Barrier">Barrier</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/barrier.rs.html#93-102' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Creates a new barrier that can block a given number of threads.</p> <p>A barrier will block <code>n</code>-1 threads which call <a href="#method.wait"><code>wait</code></a> and then wake up all threads at once when the <code>n</code>th thread calls <a href="#method.wait"><code>wait</code></a>.</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">sync</span>::<span class="ident">Barrier</span>; <span class="kw">let</span> <span class="ident">barrier</span> <span class="op">=</span> <span class="ident">Barrier</span>::<span class="ident">new</span>(<span class="number">10</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%3Async%3A%3ABarrier%3B%0A%0Alet%20barrier%20%3D%20Barrier%3A%3Anew(10)%3B%0A%7D">Run</a></pre> </div><h4 id='method.wait' class="method"><span id='wait.v' class='invisible'><code>pub fn <a href='#method.wait' class='fnname'>wait</a>(&self) -> <a class="struct" href="../../std/sync/struct.BarrierWaitResult.html" title="struct std::sync::BarrierWaitResult">BarrierWaitResult</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/barrier.rs.html#141-159' title='goto source code'>[src]</a></span></h4> <div class='docblock'><p>Blocks the current thread until all threads have rendezvoused here.</p> <p>Barriers are re-usable after all threads have rendezvoused once, and can be used continuously.</p> <p>A single (arbitrary) thread will receive a <a href="struct.BarrierWaitResult.html"><code>BarrierWaitResult</code></a> that returns <code>true</code> from <a href="struct.BarrierWaitResult.html#method.is_leader"><code>is_leader</code></a> when returning from this function, and all other threads will receive a result that will return <code>false</code> from <a href="struct.BarrierWaitResult.html#method.is_leader"><code>is_leader</code></a>.</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">sync</span>::{<span class="ident">Arc</span>, <span class="ident">Barrier</span>}; <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>; <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">handles</span> <span class="op">=</span> <span class="ident">Vec</span>::<span class="ident">with_capacity</span>(<span class="number">10</span>); <span class="kw">let</span> <span class="ident">barrier</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>(<span class="ident">Barrier</span>::<span class="ident">new</span>(<span class="number">10</span>)); <span class="kw">for</span> _ <span class="kw">in</span> <span class="number">0</span>..<span class="number">10</span> { <span class="kw">let</span> <span class="ident">c</span> <span class="op">=</span> <span class="ident">barrier</span>.<span class="ident">clone</span>(); <span class="comment">// The same messages will be printed together.</span> <span class="comment">// You will NOT see any interleaving.</span> <span class="ident">handles</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="macro">println</span><span class="macro">!</span>(<span class="string">"before wait"</span>); <span class="ident">c</span>.<span class="ident">wait</span>(); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"after wait"</span>); })); } <span class="comment">// Wait for other threads to finish.</span> <span class="kw">for</span> <span class="ident">handle</span> <span class="kw">in</span> <span class="ident">handles</span> { <span class="ident">handle</span>.<span class="ident">join</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%3Async%3A%3A%7BArc%2C%20Barrier%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20mut%20handles%20%3D%20Vec%3A%3Awith_capacity(10)%3B%0Alet%20barrier%20%3D%20Arc%3A%3Anew(Barrier%3A%3Anew(10))%3B%0Afor%20_%20in%200..10%20%7B%0A%20%20%20%20let%20c%20%3D%20barrier.clone()%3B%0A%20%20%20%20%2F%2F%20The%20same%20messages%20will%20be%20printed%20together.%0A%20%20%20%20%2F%2F%20You%20will%20NOT%20see%20any%20interleaving.%0A%20%20%20%20handles.push(thread%3A%3Aspawn(move%7C%7C%20%7B%0A%20%20%20%20%20%20%20%20println!(%22before%20wait%22)%3B%0A%20%20%20%20%20%20%20%20c.wait()%3B%0A%20%20%20%20%20%20%20%20println!(%22after%20wait%22)%3B%0A%20%20%20%20%7D))%3B%0A%7D%0A%2F%2F%20Wait%20for%20other%20threads%20to%20finish.%0Afor%20handle%20in%20handles%20%7B%0A%20%20%20%20handle.join().unwrap()%3B%0A%7D%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 <a class="trait" href="../../std/fmt/trait.Debug.html" title="trait std::fmt::Debug">Debug</a> for <a class="struct" href="../../std/sync/struct.Barrier.html" title="struct std::sync::Barrier">Barrier</a></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/sync/barrier.rs.html#71-75' 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/sync/barrier.rs.html#72-74' 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></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>