Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 564935689ab5527f955e5449ded02799 > files > 5173

rust-doc-1.19.0-1.mga6.armv7hl.rpm

<!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 `Condvar` struct in crate `std`.">
    <meta name="keywords" content="rust, rustlang, rust-lang, Condvar">

    <title>std::sync::Condvar - Rust</title>

    <link rel="stylesheet" type="text/css" href="../../normalize.css">
    <link rel="stylesheet" type="text/css" href="../../rustdoc.css">
    <link rel="stylesheet" type="text/css" href="../../main.css">
    

    <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">
        <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 Condvar</p><div class="block items"><ul><li><a href="#methods">Methods</a></li><li><a href="#implementations">Trait Implementations</a></li></ul></div><p class='location'><a href='../index.html'>std</a>::<wbr><a href='index.html'>sync</a></p><script>window.sidebarCurrent = {name: 'Condvar', 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">
<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=''>Condvar</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'>&#x2212;</span>]
                   </a>
               </span><a class='srclink' href='../../src/std/sync/condvar.rs.html#120-123' title='goto source code'>[src]</a></span></h1>
<pre class='rust struct'>pub struct Condvar { /* fields omitted */ }</pre><div class='docblock'><p>A Condition Variable</p>

<p>Condition variables represent the ability to block a thread such that it
consumes no CPU time while waiting for an event to occur. Condition
variables are typically associated with a boolean predicate (a condition)
and a mutex. The predicate is always verified inside of the mutex before
determining that a thread must block.</p>

<p>Functions in this module will block the current <strong>thread</strong> of execution and
are bindings to system-provided condition variables where possible. Note
that this module places one additional restriction over the system condition
variables: each condvar can be used with precisely one mutex at runtime. Any
attempt to use multiple mutexes on the same condition variable will result
in a runtime panic. If this is not desired, then the unsafe primitives in
<code>sys</code> do not have this restriction but may result in undefined behavior.</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">Mutex</span>, <span class="ident">Condvar</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">let</span> <span class="ident">pair</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>((<span class="ident">Mutex</span>::<span class="ident">new</span>(<span class="bool-val">false</span>), <span class="ident">Condvar</span>::<span class="ident">new</span>()));
<span class="kw">let</span> <span class="ident">pair2</span> <span class="op">=</span> <span class="ident">pair</span>.<span class="ident">clone</span>();

<span class="comment">// Inside of our lock, spawn a new thread, and then wait for it to start.</span>
<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span><span class="op">||</span> {
    <span class="kw">let</span> <span class="kw-2">&amp;</span>(<span class="kw-2">ref</span> <span class="ident">lock</span>, <span class="kw-2">ref</span> <span class="ident">cvar</span>) <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">pair2</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">started</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
    <span class="kw-2">*</span><span class="ident">started</span> <span class="op">=</span> <span class="bool-val">true</span>;
    <span class="comment">// We notify the condvar that the value has changed.</span>
    <span class="ident">cvar</span>.<span class="ident">notify_one</span>();
});

<span class="comment">// Wait for the thread to start up.</span>
<span class="kw">let</span> <span class="kw-2">&amp;</span>(<span class="kw-2">ref</span> <span class="ident">lock</span>, <span class="kw-2">ref</span> <span class="ident">cvar</span>) <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">pair</span>;
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">started</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
<span class="kw">while</span> <span class="op">!</span><span class="kw-2">*</span><span class="ident">started</span> {
    <span class="ident">started</span> <span class="op">=</span> <span class="ident">cvar</span>.<span class="ident">wait</span>(<span class="ident">started</span>).<span class="ident">unwrap</span>();
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=fn%20main()%20%7B%0Ause%20std%3A%3Async%3A%3A%7BArc%2C%20Mutex%2C%20Condvar%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20pair%20%3D%20Arc%3A%3Anew((Mutex%3A%3Anew(false)%2C%20Condvar%3A%3Anew()))%3B%0Alet%20pair2%20%3D%20pair.clone()%3B%0A%0A%2F%2F%20Inside%20of%20our%20lock%2C%20spawn%20a%20new%20thread%2C%20and%20then%20wait%20for%20it%20to%20start.%0Athread%3A%3Aspawn(move%7C%7C%20%7B%0A%20%20%20%20let%20%26(ref%20lock%2C%20ref%20cvar)%20%3D%20%26*pair2%3B%0A%20%20%20%20let%20mut%20started%20%3D%20lock.lock().unwrap()%3B%0A%20%20%20%20*started%20%3D%20true%3B%0A%20%20%20%20%2F%2F%20We%20notify%20the%20condvar%20that%20the%20value%20has%20changed.%0A%20%20%20%20cvar.notify_one()%3B%0A%7D)%3B%0A%0A%2F%2F%20Wait%20for%20the%20thread%20to%20start%20up.%0Alet%20%26(ref%20lock%2C%20ref%20cvar)%20%3D%20%26*pair%3B%0Alet%20mut%20started%20%3D%20lock.lock().unwrap()%3B%0Awhile%20!*started%20%7B%0A%20%20%20%20started%20%3D%20cvar.wait(started).unwrap()%3B%0A%7D%0A%7D">Run</a></pre>
</div><h2 id='methods'>Methods</h2><h3 class='impl'><span class='in-band'><code>impl <a class="struct" href="../../std/sync/struct.Condvar.html" title="struct std::sync::Condvar">Condvar</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/condvar.rs.html#125-455' 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>fn <a href='#method.new' class='fnname'>new</a>() -&gt; <a class="struct" href="../../std/sync/struct.Condvar.html" title="struct std::sync::Condvar">Condvar</a></code></span></h4>
<div class='docblock'><p>Creates a new condition variable which is ready to be waited on and
notified.</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">Condvar</span>;

<span class="kw">let</span> <span class="ident">condvar</span> <span class="op">=</span> <span class="ident">Condvar</span>::<span class="ident">new</span>();<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=fn%20main()%20%7B%0Ause%20std%3A%3Async%3A%3ACondvar%3B%0A%0Alet%20condvar%20%3D%20Condvar%3A%3Anew()%3B%0A%7D">Run</a></pre>
</div><h4 id='method.wait' class="method"><span id='wait.v' class='invisible'><code>fn <a href='#method.wait' class='fnname'>wait</a>&lt;'a, T&gt;(&amp;self, guard: <a class="struct" href="../../std/sync/struct.MutexGuard.html" title="struct std::sync::MutexGuard">MutexGuard</a>&lt;'a, T&gt;) -&gt; <a class="type" href="../../std/sync/type.LockResult.html" title="type std::sync::LockResult">LockResult</a>&lt;<a class="struct" href="../../std/sync/struct.MutexGuard.html" title="struct std::sync::MutexGuard">MutexGuard</a>&lt;'a, T&gt;&gt;</code></span></h4>
<div class='docblock'><p>Blocks the current thread until this condition variable receives a
notification.</p>

<p>This function will atomically unlock the mutex specified (represented by
<code>guard</code>) and block the current thread. This means that any calls
to <a href="#method.notify_one"><code>notify_one</code></a> or <a href="#method.notify_all"><code>notify_all</code></a> which happen logically after the
mutex is unlocked are candidates to wake this thread up. When this
function call returns, the lock specified will have been re-acquired.</p>

<p>Note that this function is susceptible to spurious wakeups. Condition
variables normally have a boolean predicate associated with them, and
the predicate must always be checked each time this function returns to
protect against spurious wakeups.</p>

<h1 id='errors' class='section-header'><a href='#errors'>Errors</a></h1>
<p>This function will return an error if the mutex being waited on is
poisoned when this thread re-acquires the lock. For more information,
see information about <a href="../sync/struct.Mutex.html#poisoning">poisoning</a> on the <a href="../sync/struct.Mutex.html"><code>Mutex</code></a> type.</p>

<h1 id='panics' class='section-header'><a href='#panics'>Panics</a></h1>
<p>This function will <a href="../../std/macro.panic.html"><code>panic!</code></a> if it is used with more than one mutex
over time. Each condition variable is dynamically bound to exactly one
mutex to ensure defined behavior across platforms. If this functionality
is not desired, then unsafe primitives in <code>sys</code> are provided.</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">Mutex</span>, <span class="ident">Condvar</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">let</span> <span class="ident">pair</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>((<span class="ident">Mutex</span>::<span class="ident">new</span>(<span class="bool-val">false</span>), <span class="ident">Condvar</span>::<span class="ident">new</span>()));
<span class="kw">let</span> <span class="ident">pair2</span> <span class="op">=</span> <span class="ident">pair</span>.<span class="ident">clone</span>();

<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span><span class="op">||</span> {
    <span class="kw">let</span> <span class="kw-2">&amp;</span>(<span class="kw-2">ref</span> <span class="ident">lock</span>, <span class="kw-2">ref</span> <span class="ident">cvar</span>) <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">pair2</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">started</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
    <span class="kw-2">*</span><span class="ident">started</span> <span class="op">=</span> <span class="bool-val">true</span>;
    <span class="comment">// We notify the condvar that the value has changed.</span>
    <span class="ident">cvar</span>.<span class="ident">notify_one</span>();
});

<span class="comment">// Wait for the thread to start up.</span>
<span class="kw">let</span> <span class="kw-2">&amp;</span>(<span class="kw-2">ref</span> <span class="ident">lock</span>, <span class="kw-2">ref</span> <span class="ident">cvar</span>) <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">pair</span>;
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">started</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
<span class="comment">// As long as the value inside the `Mutex` is false, we wait.</span>
<span class="kw">while</span> <span class="op">!</span><span class="kw-2">*</span><span class="ident">started</span> {
    <span class="ident">started</span> <span class="op">=</span> <span class="ident">cvar</span>.<span class="ident">wait</span>(<span class="ident">started</span>).<span class="ident">unwrap</span>();
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=fn%20main()%20%7B%0Ause%20std%3A%3Async%3A%3A%7BArc%2C%20Mutex%2C%20Condvar%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20pair%20%3D%20Arc%3A%3Anew((Mutex%3A%3Anew(false)%2C%20Condvar%3A%3Anew()))%3B%0Alet%20pair2%20%3D%20pair.clone()%3B%0A%0Athread%3A%3Aspawn(move%7C%7C%20%7B%0A%20%20%20%20let%20%26(ref%20lock%2C%20ref%20cvar)%20%3D%20%26*pair2%3B%0A%20%20%20%20let%20mut%20started%20%3D%20lock.lock().unwrap()%3B%0A%20%20%20%20*started%20%3D%20true%3B%0A%20%20%20%20%2F%2F%20We%20notify%20the%20condvar%20that%20the%20value%20has%20changed.%0A%20%20%20%20cvar.notify_one()%3B%0A%7D)%3B%0A%0A%2F%2F%20Wait%20for%20the%20thread%20to%20start%20up.%0Alet%20%26(ref%20lock%2C%20ref%20cvar)%20%3D%20%26*pair%3B%0Alet%20mut%20started%20%3D%20lock.lock().unwrap()%3B%0A%2F%2F%20As%20long%20as%20the%20value%20inside%20the%20%60Mutex%60%20is%20false%2C%20we%20wait.%0Awhile%20!*started%20%7B%0A%20%20%20%20started%20%3D%20cvar.wait(started).unwrap()%3B%0A%7D%0A%7D">Run</a></pre>
</div><h4 id='method.wait_timeout_ms' class="method"><span id='wait_timeout_ms.v' class='invisible'><code>fn <a href='#method.wait_timeout_ms' class='fnname'>wait_timeout_ms</a>&lt;'a, T&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&amp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;guard: <a class="struct" href="../../std/sync/struct.MutexGuard.html" title="struct std::sync::MutexGuard">MutexGuard</a>&lt;'a, T&gt;, <br>&nbsp;&nbsp;&nbsp;&nbsp;ms: <a class="primitive" href="../primitive.u32.html">u32</a><br>) -&gt; <a class="type" href="../../std/sync/type.LockResult.html" title="type std::sync::LockResult">LockResult</a>&lt;<a class="primitive" href="../primitive.tuple.html">(</a><a class="struct" href="../../std/sync/struct.MutexGuard.html" title="struct std::sync::MutexGuard">MutexGuard</a>&lt;'a, T&gt;, <a class="primitive" href="../primitive.bool.html">bool</a><a class="primitive" href="../primitive.tuple.html">)</a>&gt;</code></span></h4>
<div class='stability'><div class='stab deprecated'>Deprecated since 1.6.0<p>: replaced by <code>std::sync::Condvar::wait_timeout</code></p>
</div></div><div class='docblock'><p>Waits on this condition variable for a notification, timing out after a
specified duration.</p>

<p>The semantics of this function are equivalent to <a href="#method.wait"><code>wait</code></a>
except that the thread will be blocked for roughly no longer
than <code>ms</code> milliseconds. This method should not be used for
precise timing due to anomalies such as preemption or platform
differences that may not cause the maximum amount of time
waited to be precisely <code>ms</code>.</p>

<p>Note that the best effort is made to ensure that the time waited is
measured with a monotonic clock, and not affected by the changes made to
the system time.</p>

<p>The returned boolean is <code>false</code> only if the timeout is known
to have elapsed.</p>

<p>Like <a href="#method.wait"><code>wait</code></a>, the lock specified will be re-acquired when this function
returns, regardless of whether the timeout elapsed or not.</p>

<h1 id='examples-3' class='section-header'><a href='#examples-3'>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">Mutex</span>, <span class="ident">Condvar</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">let</span> <span class="ident">pair</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>((<span class="ident">Mutex</span>::<span class="ident">new</span>(<span class="bool-val">false</span>), <span class="ident">Condvar</span>::<span class="ident">new</span>()));
<span class="kw">let</span> <span class="ident">pair2</span> <span class="op">=</span> <span class="ident">pair</span>.<span class="ident">clone</span>();

<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span><span class="op">||</span> {
    <span class="kw">let</span> <span class="kw-2">&amp;</span>(<span class="kw-2">ref</span> <span class="ident">lock</span>, <span class="kw-2">ref</span> <span class="ident">cvar</span>) <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">pair2</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">started</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
    <span class="kw-2">*</span><span class="ident">started</span> <span class="op">=</span> <span class="bool-val">true</span>;
    <span class="comment">// We notify the condvar that the value has changed.</span>
    <span class="ident">cvar</span>.<span class="ident">notify_one</span>();
});

<span class="comment">// Wait for the thread to start up.</span>
<span class="kw">let</span> <span class="kw-2">&amp;</span>(<span class="kw-2">ref</span> <span class="ident">lock</span>, <span class="kw-2">ref</span> <span class="ident">cvar</span>) <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">pair</span>;
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">started</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
<span class="comment">// As long as the value inside the `Mutex` is false, we wait.</span>
<span class="kw">loop</span> {
    <span class="kw">let</span> <span class="ident">result</span> <span class="op">=</span> <span class="ident">cvar</span>.<span class="ident">wait_timeout_ms</span>(<span class="ident">started</span>, <span class="number">10</span>).<span class="ident">unwrap</span>();
    <span class="comment">// 10 milliseconds have passed, or maybe the value changed!</span>
    <span class="ident">started</span> <span class="op">=</span> <span class="ident">result</span>.<span class="number">0</span>;
    <span class="kw">if</span> <span class="kw-2">*</span><span class="ident">started</span> <span class="op">==</span> <span class="bool-val">true</span> {
        <span class="comment">// We received the notification and the value has been updated, we can leave.</span>
        <span class="kw">break</span>
    }
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=fn%20main()%20%7B%0Ause%20std%3A%3Async%3A%3A%7BArc%2C%20Mutex%2C%20Condvar%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20pair%20%3D%20Arc%3A%3Anew((Mutex%3A%3Anew(false)%2C%20Condvar%3A%3Anew()))%3B%0Alet%20pair2%20%3D%20pair.clone()%3B%0A%0Athread%3A%3Aspawn(move%7C%7C%20%7B%0A%20%20%20%20let%20%26(ref%20lock%2C%20ref%20cvar)%20%3D%20%26*pair2%3B%0A%20%20%20%20let%20mut%20started%20%3D%20lock.lock().unwrap()%3B%0A%20%20%20%20*started%20%3D%20true%3B%0A%20%20%20%20%2F%2F%20We%20notify%20the%20condvar%20that%20the%20value%20has%20changed.%0A%20%20%20%20cvar.notify_one()%3B%0A%7D)%3B%0A%0A%2F%2F%20Wait%20for%20the%20thread%20to%20start%20up.%0Alet%20%26(ref%20lock%2C%20ref%20cvar)%20%3D%20%26*pair%3B%0Alet%20mut%20started%20%3D%20lock.lock().unwrap()%3B%0A%2F%2F%20As%20long%20as%20the%20value%20inside%20the%20%60Mutex%60%20is%20false%2C%20we%20wait.%0Aloop%20%7B%0A%20%20%20%20let%20result%20%3D%20cvar.wait_timeout_ms(started%2C%2010).unwrap()%3B%0A%20%20%20%20%2F%2F%2010%20milliseconds%20have%20passed%2C%20or%20maybe%20the%20value%20changed!%0A%20%20%20%20started%20%3D%20result.0%3B%0A%20%20%20%20if%20*started%20%3D%3D%20true%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20We%20received%20the%20notification%20and%20the%20value%20has%20been%20updated%2C%20we%20can%20leave.%0A%20%20%20%20%20%20%20%20break%0A%20%20%20%20%7D%0A%7D%0A%7D">Run</a></pre>
</div><h4 id='method.wait_timeout' class="method"><span id='wait_timeout.v' class='invisible'><code>fn <a href='#method.wait_timeout' class='fnname'>wait_timeout</a>&lt;'a, T&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&amp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;guard: <a class="struct" href="../../std/sync/struct.MutexGuard.html" title="struct std::sync::MutexGuard">MutexGuard</a>&lt;'a, T&gt;, <br>&nbsp;&nbsp;&nbsp;&nbsp;dur: <a class="struct" href="../../std/time/struct.Duration.html" title="struct std::time::Duration">Duration</a><br>) -&gt; <a class="type" href="../../std/sync/type.LockResult.html" title="type std::sync::LockResult">LockResult</a>&lt;<a class="primitive" href="../primitive.tuple.html">(</a><a class="struct" href="../../std/sync/struct.MutexGuard.html" title="struct std::sync::MutexGuard">MutexGuard</a>&lt;'a, T&gt;, <a class="struct" href="../../std/sync/struct.WaitTimeoutResult.html" title="struct std::sync::WaitTimeoutResult">WaitTimeoutResult</a><a class="primitive" href="../primitive.tuple.html">)</a>&gt;</code><div class='since' title='Stable since Rust version 1.5.0'>1.5.0</div></span></h4>
<div class='docblock'><p>Waits on this condition variable for a notification, timing out after a
specified duration.</p>

<p>The semantics of this function are equivalent to <a href="#method.wait"><code>wait</code></a> except that
the thread will be blocked for roughly no longer than <code>dur</code>. This
method should not be used for precise timing due to anomalies such as
preemption or platform differences that may not cause the maximum
amount of time waited to be precisely <code>dur</code>.</p>

<p>Note that the best effort is made to ensure that the time waited is
measured with a monotonic clock, and not affected by the changes made to
the system time.</p>

<p>The returned <a href="struct.WaitTimeoutResult.html"><code>WaitTimeoutResult</code></a> value indicates if the timeout is
known to have elapsed.</p>

<p>Like <a href="#method.wait"><code>wait</code></a>, the lock specified will be re-acquired when this function
returns, regardless of whether the timeout elapsed or not.</p>

<h1 id='examples-4' class='section-header'><a href='#examples-4'>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">Mutex</span>, <span class="ident">Condvar</span>};
<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">pair</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>((<span class="ident">Mutex</span>::<span class="ident">new</span>(<span class="bool-val">false</span>), <span class="ident">Condvar</span>::<span class="ident">new</span>()));
<span class="kw">let</span> <span class="ident">pair2</span> <span class="op">=</span> <span class="ident">pair</span>.<span class="ident">clone</span>();

<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span><span class="op">||</span> {
    <span class="kw">let</span> <span class="kw-2">&amp;</span>(<span class="kw-2">ref</span> <span class="ident">lock</span>, <span class="kw-2">ref</span> <span class="ident">cvar</span>) <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">pair2</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">started</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
    <span class="kw-2">*</span><span class="ident">started</span> <span class="op">=</span> <span class="bool-val">true</span>;
    <span class="comment">// We notify the condvar that the value has changed.</span>
    <span class="ident">cvar</span>.<span class="ident">notify_one</span>();
});

<span class="comment">// wait for the thread to start up</span>
<span class="kw">let</span> <span class="kw-2">&amp;</span>(<span class="kw-2">ref</span> <span class="ident">lock</span>, <span class="kw-2">ref</span> <span class="ident">cvar</span>) <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">pair</span>;
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">started</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
<span class="comment">// as long as the value inside the `Mutex` is false, we wait</span>
<span class="kw">loop</span> {
    <span class="kw">let</span> <span class="ident">result</span> <span class="op">=</span> <span class="ident">cvar</span>.<span class="ident">wait_timeout</span>(<span class="ident">started</span>, <span class="ident">Duration</span>::<span class="ident">from_millis</span>(<span class="number">10</span>)).<span class="ident">unwrap</span>();
    <span class="comment">// 10 milliseconds have passed, or maybe the value changed!</span>
    <span class="ident">started</span> <span class="op">=</span> <span class="ident">result</span>.<span class="number">0</span>;
    <span class="kw">if</span> <span class="kw-2">*</span><span class="ident">started</span> <span class="op">==</span> <span class="bool-val">true</span> {
        <span class="comment">// We received the notification and the value has been updated, we can leave.</span>
        <span class="kw">break</span>
    }
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=fn%20main()%20%7B%0Ause%20std%3A%3Async%3A%3A%7BArc%2C%20Mutex%2C%20Condvar%7D%3B%0Ause%20std%3A%3Athread%3B%0Ause%20std%3A%3Atime%3A%3ADuration%3B%0A%0Alet%20pair%20%3D%20Arc%3A%3Anew((Mutex%3A%3Anew(false)%2C%20Condvar%3A%3Anew()))%3B%0Alet%20pair2%20%3D%20pair.clone()%3B%0A%0Athread%3A%3Aspawn(move%7C%7C%20%7B%0A%20%20%20%20let%20%26(ref%20lock%2C%20ref%20cvar)%20%3D%20%26*pair2%3B%0A%20%20%20%20let%20mut%20started%20%3D%20lock.lock().unwrap()%3B%0A%20%20%20%20*started%20%3D%20true%3B%0A%20%20%20%20%2F%2F%20We%20notify%20the%20condvar%20that%20the%20value%20has%20changed.%0A%20%20%20%20cvar.notify_one()%3B%0A%7D)%3B%0A%0A%2F%2F%20wait%20for%20the%20thread%20to%20start%20up%0Alet%20%26(ref%20lock%2C%20ref%20cvar)%20%3D%20%26*pair%3B%0Alet%20mut%20started%20%3D%20lock.lock().unwrap()%3B%0A%2F%2F%20as%20long%20as%20the%20value%20inside%20the%20%60Mutex%60%20is%20false%2C%20we%20wait%0Aloop%20%7B%0A%20%20%20%20let%20result%20%3D%20cvar.wait_timeout(started%2C%20Duration%3A%3Afrom_millis(10)).unwrap()%3B%0A%20%20%20%20%2F%2F%2010%20milliseconds%20have%20passed%2C%20or%20maybe%20the%20value%20changed!%0A%20%20%20%20started%20%3D%20result.0%3B%0A%20%20%20%20if%20*started%20%3D%3D%20true%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20We%20received%20the%20notification%20and%20the%20value%20has%20been%20updated%2C%20we%20can%20leave.%0A%20%20%20%20%20%20%20%20break%0A%20%20%20%20%7D%0A%7D%0A%7D">Run</a></pre>
</div><h4 id='method.notify_one' class="method"><span id='notify_one.v' class='invisible'><code>fn <a href='#method.notify_one' class='fnname'>notify_one</a>(&amp;self)</code></span></h4>
<div class='docblock'><p>Wakes up one blocked thread on this condvar.</p>

<p>If there is a blocked thread on this condition variable, then it will
be woken up from its call to <a href="#method.wait"><code>wait</code></a> or <a href="#method.wait_timeout"><code>wait_timeout</code></a>. Calls to
<code>notify_one</code> are not buffered in any way.</p>

<p>To wake up all threads, see <a href="#method.notify_all"><code>notify_all</code></a>.</p>

<h1 id='examples-5' class='section-header'><a href='#examples-5'>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">Mutex</span>, <span class="ident">Condvar</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">let</span> <span class="ident">pair</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>((<span class="ident">Mutex</span>::<span class="ident">new</span>(<span class="bool-val">false</span>), <span class="ident">Condvar</span>::<span class="ident">new</span>()));
<span class="kw">let</span> <span class="ident">pair2</span> <span class="op">=</span> <span class="ident">pair</span>.<span class="ident">clone</span>();

<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span><span class="op">||</span> {
    <span class="kw">let</span> <span class="kw-2">&amp;</span>(<span class="kw-2">ref</span> <span class="ident">lock</span>, <span class="kw-2">ref</span> <span class="ident">cvar</span>) <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">pair2</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">started</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
    <span class="kw-2">*</span><span class="ident">started</span> <span class="op">=</span> <span class="bool-val">true</span>;
    <span class="comment">// We notify the condvar that the value has changed.</span>
    <span class="ident">cvar</span>.<span class="ident">notify_one</span>();
});

<span class="comment">// Wait for the thread to start up.</span>
<span class="kw">let</span> <span class="kw-2">&amp;</span>(<span class="kw-2">ref</span> <span class="ident">lock</span>, <span class="kw-2">ref</span> <span class="ident">cvar</span>) <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">pair</span>;
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">started</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
<span class="comment">// As long as the value inside the `Mutex` is false, we wait.</span>
<span class="kw">while</span> <span class="op">!</span><span class="kw-2">*</span><span class="ident">started</span> {
    <span class="ident">started</span> <span class="op">=</span> <span class="ident">cvar</span>.<span class="ident">wait</span>(<span class="ident">started</span>).<span class="ident">unwrap</span>();
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=fn%20main()%20%7B%0Ause%20std%3A%3Async%3A%3A%7BArc%2C%20Mutex%2C%20Condvar%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20pair%20%3D%20Arc%3A%3Anew((Mutex%3A%3Anew(false)%2C%20Condvar%3A%3Anew()))%3B%0Alet%20pair2%20%3D%20pair.clone()%3B%0A%0Athread%3A%3Aspawn(move%7C%7C%20%7B%0A%20%20%20%20let%20%26(ref%20lock%2C%20ref%20cvar)%20%3D%20%26*pair2%3B%0A%20%20%20%20let%20mut%20started%20%3D%20lock.lock().unwrap()%3B%0A%20%20%20%20*started%20%3D%20true%3B%0A%20%20%20%20%2F%2F%20We%20notify%20the%20condvar%20that%20the%20value%20has%20changed.%0A%20%20%20%20cvar.notify_one()%3B%0A%7D)%3B%0A%0A%2F%2F%20Wait%20for%20the%20thread%20to%20start%20up.%0Alet%20%26(ref%20lock%2C%20ref%20cvar)%20%3D%20%26*pair%3B%0Alet%20mut%20started%20%3D%20lock.lock().unwrap()%3B%0A%2F%2F%20As%20long%20as%20the%20value%20inside%20the%20%60Mutex%60%20is%20false%2C%20we%20wait.%0Awhile%20!*started%20%7B%0A%20%20%20%20started%20%3D%20cvar.wait(started).unwrap()%3B%0A%7D%0A%7D">Run</a></pre>
</div><h4 id='method.notify_all' class="method"><span id='notify_all.v' class='invisible'><code>fn <a href='#method.notify_all' class='fnname'>notify_all</a>(&amp;self)</code></span></h4>
<div class='docblock'><p>Wakes up all blocked threads on this condvar.</p>

<p>This method will ensure that any current waiters on the condition
variable are awoken. Calls to <code>notify_all()</code> are not buffered in any
way.</p>

<p>To wake up only one thread, see <a href="#method.notify_one"><code>notify_one</code></a>.</p>

<h1 id='examples-6' class='section-header'><a href='#examples-6'>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">Mutex</span>, <span class="ident">Condvar</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">let</span> <span class="ident">pair</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>((<span class="ident">Mutex</span>::<span class="ident">new</span>(<span class="bool-val">false</span>), <span class="ident">Condvar</span>::<span class="ident">new</span>()));
<span class="kw">let</span> <span class="ident">pair2</span> <span class="op">=</span> <span class="ident">pair</span>.<span class="ident">clone</span>();

<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span><span class="op">||</span> {
    <span class="kw">let</span> <span class="kw-2">&amp;</span>(<span class="kw-2">ref</span> <span class="ident">lock</span>, <span class="kw-2">ref</span> <span class="ident">cvar</span>) <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">pair2</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">started</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
    <span class="kw-2">*</span><span class="ident">started</span> <span class="op">=</span> <span class="bool-val">true</span>;
    <span class="comment">// We notify the condvar that the value has changed.</span>
    <span class="ident">cvar</span>.<span class="ident">notify_all</span>();
});

<span class="comment">// Wait for the thread to start up.</span>
<span class="kw">let</span> <span class="kw-2">&amp;</span>(<span class="kw-2">ref</span> <span class="ident">lock</span>, <span class="kw-2">ref</span> <span class="ident">cvar</span>) <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">*</span><span class="ident">pair</span>;
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">started</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
<span class="comment">// As long as the value inside the `Mutex` is false, we wait.</span>
<span class="kw">while</span> <span class="op">!</span><span class="kw-2">*</span><span class="ident">started</span> {
    <span class="ident">started</span> <span class="op">=</span> <span class="ident">cvar</span>.<span class="ident">wait</span>(<span class="ident">started</span>).<span class="ident">unwrap</span>();
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=fn%20main()%20%7B%0Ause%20std%3A%3Async%3A%3A%7BArc%2C%20Mutex%2C%20Condvar%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20pair%20%3D%20Arc%3A%3Anew((Mutex%3A%3Anew(false)%2C%20Condvar%3A%3Anew()))%3B%0Alet%20pair2%20%3D%20pair.clone()%3B%0A%0Athread%3A%3Aspawn(move%7C%7C%20%7B%0A%20%20%20%20let%20%26(ref%20lock%2C%20ref%20cvar)%20%3D%20%26*pair2%3B%0A%20%20%20%20let%20mut%20started%20%3D%20lock.lock().unwrap()%3B%0A%20%20%20%20*started%20%3D%20true%3B%0A%20%20%20%20%2F%2F%20We%20notify%20the%20condvar%20that%20the%20value%20has%20changed.%0A%20%20%20%20cvar.notify_all()%3B%0A%7D)%3B%0A%0A%2F%2F%20Wait%20for%20the%20thread%20to%20start%20up.%0Alet%20%26(ref%20lock%2C%20ref%20cvar)%20%3D%20%26*pair%3B%0Alet%20mut%20started%20%3D%20lock.lock().unwrap()%3B%0A%2F%2F%20As%20long%20as%20the%20value%20inside%20the%20%60Mutex%60%20is%20false%2C%20we%20wait.%0Awhile%20!*started%20%7B%0A%20%20%20%20started%20%3D%20cvar.wait(started).unwrap()%3B%0A%7D%0A%7D">Run</a></pre>
</div></div><h2 id='implementations'>Trait Implementations</h2><h3 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.Condvar.html" title="struct std::sync::Condvar">Condvar</a></code></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/condvar.rs.html#458-462' 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>(&amp;self, f: &amp;mut <a class="struct" href="../../std/fmt/struct.Formatter.html" title="struct std::fmt::Formatter">Formatter</a>) -&gt; <a class="type" href="../../std/fmt/type.Result.html" title="type std::fmt::Result">Result</a></code></span></h4>
<div class='docblock'><p>Formats the value using the given formatter.</p>
</div></div><h3 class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/default/trait.Default.html" title="trait std::default::Default">Default</a> for <a class="struct" href="../../std/sync/struct.Condvar.html" title="struct std::sync::Condvar">Condvar</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.10.0'>1.10.0</div><a class='srclink' href='../../src/std/sync/condvar.rs.html#465-470' title='goto source code'>[src]</a></span></h3>
<div class='impl-items'><h4 id='method.default' class="method"><span id='default.v' class='invisible'><code>fn <a href='../../std/default/trait.Default.html#tymethod.default' class='fnname'>default</a>() -&gt; <a class="struct" href="../../std/sync/struct.Condvar.html" title="struct std::sync::Condvar">Condvar</a></code></span></h4>
<div class='docblock'><p>Creates a <code>Condvar</code> which is ready to be waited on and notified.</p>
</div></div><h3 class='impl'><span class='in-band'><code>impl <a class="trait" href="../../std/ops/trait.Drop.html" title="trait std::ops::Drop">Drop</a> for <a class="struct" href="../../std/sync/struct.Condvar.html" title="struct std::sync::Condvar">Condvar</a></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/condvar.rs.html#473-477' title='goto source code'>[src]</a></span></h3>
<div class='impl-items'><h4 id='method.drop' class="method"><span id='drop.v' class='invisible'><code>fn <a href='../../std/ops/trait.Drop.html#tymethod.drop' class='fnname'>drop</a>(&amp;mut self)</code></span></h4>
<div class='docblock'><p>A method called when the value goes out of scope. <a href="../../std/ops/trait.Drop.html#tymethod.drop">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>?</dt>
                    <dd>Show this help dialog</dd>
                    <dt>S</dt>
                    <dd>Focus the search field</dd>
                    <dt>&larrb;</dt>
                    <dd>Move up in search results</dd>
                    <dt>&rarrb;</dt>
                    <dd>Move down in search results</dd>
                    <dt>&#9166;</dt>
                    <dd>Go to active search result</dd>
                    <dt>+</dt>
                    <dd>Collapse/expand 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>