Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > fdedb3cf2140df9b6d419a2338ab1caa > files > 5911

rust-doc-1.25.0-1.mga6.x86_64.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 `Mutex` struct in crate `std`.">
    <meta name="keywords" content="rust, rustlang, rust-lang, Mutex">

    <title>std::sync::Mutex - 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">&#9776;</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 Mutex</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.lock">lock</a><a href="#method.try_lock">try_lock</a><a href="#method.is_poisoned">is_poisoned</a><a href="#method.into_inner">into_inner</a><a href="#method.get_mut">get_mut</a></div><a class="sidebar-title" href="#implementations">Trait Implementations</a><div class="sidebar-links"><a href="#impl-UnwindSafe">UnwindSafe</a><a href="#impl-RefUnwindSafe">RefUnwindSafe</a><a href="#impl-Send">Send</a><a href="#impl-Sync">Sync</a><a href="#impl-Drop">Drop</a><a href="#impl-From%3CT%3E">From&lt;T&gt;</a><a href="#impl-Default">Default</a><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: 'Mutex', 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=''>Mutex</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/mutex.rs.html#121-130' title='goto source code'>[src]</a></span></h1>
<pre class='rust struct'>pub struct Mutex&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; { /* fields omitted */ }</pre><div class='docblock'><p>A mutual exclusion primitive useful for protecting shared data</p>
<p>This mutex will block threads waiting for the lock to become available. The
mutex can also be statically initialized or created via a <a href="#method.new"><code>new</code></a>
constructor. Each mutex has a type parameter which represents the data that
it is protecting. The data can only be accessed through the RAII guards
returned from <a href="#method.lock"><code>lock</code></a> and <a href="#method.try_lock"><code>try_lock</code></a>, which guarantees that the data is only
ever accessed when the mutex is locked.</p>
<h1 id="poisoning" class="section-header"><a href="#poisoning">Poisoning</a></h1>
<p>The mutexes in this module implement a strategy called &quot;poisoning&quot; where a
mutex is considered poisoned whenever a thread panics while holding the
mutex. Once a mutex is poisoned, all other threads are unable to access the
data by default as it is likely tainted (some invariant is not being
upheld).</p>
<p>For a mutex, this means that the <a href="#method.lock"><code>lock</code></a> and <a href="#method.try_lock"><code>try_lock</code></a> methods return a
<a href="../../std/result/enum.Result.html"><code>Result</code></a> which indicates whether a mutex has been poisoned or not. Most
usage of a mutex will simply <a href="../../std/result/enum.Result.html#method.unwrap"><code>unwrap()</code></a> these results, propagating panics
among threads to ensure that a possibly invalid invariant is not witnessed.</p>
<p>A poisoned mutex, however, does not prevent all access to the underlying
data. The <a href="../../std/sync/struct.PoisonError.html"><code>PoisonError</code></a> type has an <a href="../../std/sync/struct.PoisonError.html#method.into_inner"><code>into_inner</code></a> method which will return
the guard that would have otherwise been returned on a successful lock. This
allows access to the data, despite the lock being poisoned.</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="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">sync</span>::<span class="ident">mpsc</span>::<span class="ident">channel</span>;

<span class="kw">const</span> <span class="ident">N</span>: <span class="ident">usize</span> <span class="op">=</span> <span class="number">10</span>;

<span class="comment">// Spawn a few threads to increment a shared variable (non-atomically), and</span>
<span class="comment">// let the main thread know once all increments are done.</span>
<span class="comment">//</span>
<span class="comment">// Here we&#39;re using an Arc to share memory among threads, and the data inside</span>
<span class="comment">// the Arc is protected with a mutex.</span>
<span class="kw">let</span> <span class="ident">data</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="number">0</span>));

<span class="kw">let</span> (<span class="ident">tx</span>, <span class="ident">rx</span>) <span class="op">=</span> <span class="ident">channel</span>();
<span class="kw">for</span> _ <span class="kw">in</span> <span class="number">0</span>..<span class="ident">N</span> {
    <span class="kw">let</span> (<span class="ident">data</span>, <span class="ident">tx</span>) <span class="op">=</span> (<span class="ident">data</span>.<span class="ident">clone</span>(), <span class="ident">tx</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="comment">// The shared state can only be accessed once the lock is held.</span>
        <span class="comment">// Our non-atomic increment is safe because we&#39;re the only thread</span>
        <span class="comment">// which can access the shared state when the lock is held.</span>
        <span class="comment">//</span>
        <span class="comment">// We unwrap() the return value to assert that we are not expecting</span>
        <span class="comment">// threads to ever fail while holding the lock.</span>
        <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">data</span> <span class="op">=</span> <span class="ident">data</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
        <span class="kw-2">*</span><span class="ident">data</span> <span class="op">+=</span> <span class="number">1</span>;
        <span class="kw">if</span> <span class="kw-2">*</span><span class="ident">data</span> <span class="op">==</span> <span class="ident">N</span> {
            <span class="ident">tx</span>.<span class="ident">send</span>(()).<span class="ident">unwrap</span>();
        }
        <span class="comment">// the lock is unlocked here when `data` goes out of scope.</span>
    });
}

<span class="ident">rx</span>.<span class="ident">recv</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%20Mutex%7D%3B%0Ause%20std%3A%3Athread%3B%0Ause%20std%3A%3Async%3A%3Ampsc%3A%3Achannel%3B%0A%0Aconst%20N%3A%20usize%20%3D%2010%3B%0A%0A%2F%2F%20Spawn%20a%20few%20threads%20to%20increment%20a%20shared%20variable%20(non-atomically)%2C%20and%0A%2F%2F%20let%20the%20main%20thread%20know%20once%20all%20increments%20are%20done.%0A%2F%2F%0A%2F%2F%20Here%20we're%20using%20an%20Arc%20to%20share%20memory%20among%20threads%2C%20and%20the%20data%20inside%0A%2F%2F%20the%20Arc%20is%20protected%20with%20a%20mutex.%0Alet%20data%20%3D%20Arc%3A%3Anew(Mutex%3A%3Anew(0))%3B%0A%0Alet%20(tx%2C%20rx)%20%3D%20channel()%3B%0Afor%20_%20in%200..N%20%7B%0A%20%20%20%20let%20(data%2C%20tx)%20%3D%20(data.clone()%2C%20tx.clone())%3B%0A%20%20%20%20thread%3A%3Aspawn(move%20%7C%7C%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20The%20shared%20state%20can%20only%20be%20accessed%20once%20the%20lock%20is%20held.%0A%20%20%20%20%20%20%20%20%2F%2F%20Our%20non-atomic%20increment%20is%20safe%20because%20we're%20the%20only%20thread%0A%20%20%20%20%20%20%20%20%2F%2F%20which%20can%20access%20the%20shared%20state%20when%20the%20lock%20is%20held.%0A%20%20%20%20%20%20%20%20%2F%2F%0A%20%20%20%20%20%20%20%20%2F%2F%20We%20unwrap()%20the%20return%20value%20to%20assert%20that%20we%20are%20not%20expecting%0A%20%20%20%20%20%20%20%20%2F%2F%20threads%20to%20ever%20fail%20while%20holding%20the%20lock.%0A%20%20%20%20%20%20%20%20let%20mut%20data%20%3D%20data.lock().unwrap()%3B%0A%20%20%20%20%20%20%20%20*data%20%2B%3D%201%3B%0A%20%20%20%20%20%20%20%20if%20*data%20%3D%3D%20N%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20tx.send(()).unwrap()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%2F%2F%20the%20lock%20is%20unlocked%20here%20when%20%60data%60%20goes%20out%20of%20scope.%0A%20%20%20%20%7D)%3B%0A%7D%0A%0Arx.recv().unwrap()%3B%0A%7D">Run</a></pre>
<p>To recover from a poisoned mutex:</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="ident">Mutex</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">let</span> <span class="ident">lock</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="number">0_u32</span>));
<span class="kw">let</span> <span class="ident">lock2</span> <span class="op">=</span> <span class="ident">lock</span>.<span class="ident">clone</span>();

<span class="kw">let</span> _ <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">||</span> <span class="op">-&gt;</span> () {
    <span class="comment">// This thread will acquire the mutex first, unwrapping the result of</span>
    <span class="comment">// `lock` because the lock has not been poisoned.</span>
    <span class="kw">let</span> <span class="ident">_guard</span> <span class="op">=</span> <span class="ident">lock2</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();

    <span class="comment">// This panic while holding the lock (`_guard` is in scope) will poison</span>
    <span class="comment">// the mutex.</span>
    <span class="macro">panic</span><span class="macro">!</span>();
}).<span class="ident">join</span>();

<span class="comment">// The lock is poisoned by this point, but the returned result can be</span>
<span class="comment">// pattern matched on to return the underlying guard on both branches.</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">guard</span> <span class="op">=</span> <span class="kw">match</span> <span class="ident">lock</span>.<span class="ident">lock</span>() {
    <span class="prelude-val">Ok</span>(<span class="ident">guard</span>) <span class="op">=&gt;</span> <span class="ident">guard</span>,
    <span class="prelude-val">Err</span>(<span class="ident">poisoned</span>) <span class="op">=&gt;</span> <span class="ident">poisoned</span>.<span class="ident">into_inner</span>(),
};

<span class="kw-2">*</span><span class="ident">guard</span> <span class="op">+=</span> <span class="number">1</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%20Mutex%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20lock%20%3D%20Arc%3A%3Anew(Mutex%3A%3Anew(0_u32))%3B%0Alet%20lock2%20%3D%20lock.clone()%3B%0A%0Alet%20_%20%3D%20thread%3A%3Aspawn(move%20%7C%7C%20-%3E%20()%20%7B%0A%20%20%20%20%2F%2F%20This%20thread%20will%20acquire%20the%20mutex%20first%2C%20unwrapping%20the%20result%20of%0A%20%20%20%20%2F%2F%20%60lock%60%20because%20the%20lock%20has%20not%20been%20poisoned.%0A%20%20%20%20let%20_guard%20%3D%20lock2.lock().unwrap()%3B%0A%0A%20%20%20%20%2F%2F%20This%20panic%20while%20holding%20the%20lock%20(%60_guard%60%20is%20in%20scope)%20will%20poison%0A%20%20%20%20%2F%2F%20the%20mutex.%0A%20%20%20%20panic!()%3B%0A%7D).join()%3B%0A%0A%2F%2F%20The%20lock%20is%20poisoned%20by%20this%20point%2C%20but%20the%20returned%20result%20can%20be%0A%2F%2F%20pattern%20matched%20on%20to%20return%20the%20underlying%20guard%20on%20both%20branches.%0Alet%20mut%20guard%20%3D%20match%20lock.lock()%20%7B%0A%20%20%20%20Ok(guard)%20%3D%3E%20guard%2C%0A%20%20%20%20Err(poisoned)%20%3D%3E%20poisoned.into_inner()%2C%0A%7D%3B%0A%0A*guard%20%2B%3D%201%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&lt;T&gt; <a class="struct" href="../../std/sync/struct.Mutex.html" title="struct std::sync::Mutex">Mutex</a>&lt;T&gt;</code><a href='#impl' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/mutex.rs.html#167-189' 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>(t: T) -&gt; <a class="struct" href="../../std/sync/struct.Mutex.html" title="struct std::sync::Mutex">Mutex</a>&lt;T&gt;</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/mutex.rs.html#178-188' title='goto source code'>[src]</a></span></h4>
<div class='docblock'><p>Creates a new mutex in an unlocked state ready for use.</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">Mutex</span>;

<span class="kw">let</span> <span class="ident">mutex</span> <span class="op">=</span> <span class="ident">Mutex</span>::<span class="ident">new</span>(<span class="number">0</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%3AMutex%3B%0A%0Alet%20mutex%20%3D%20Mutex%3A%3Anew(0)%3B%0A%7D">Run</a></pre>
</div></div><h3 id='impl-1' class='impl'><span class='in-band'><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; <a class="struct" href="../../std/sync/struct.Mutex.html" title="struct std::sync::Mutex">Mutex</a>&lt;T&gt;</code><a href='#impl-1' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/mutex.rs.html#191-371' title='goto source code'>[src]</a></span></h3>
<div class='impl-items'><h4 id='method.lock' class="method"><span id='lock.v' class='invisible'><code>pub fn <a href='#method.lock' class='fnname'>lock</a>(&amp;self) -&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;T&gt;&gt;</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/mutex.rs.html#228-233' title='goto source code'>[src]</a></span></h4>
<div class='docblock'><p>Acquires a mutex, blocking the current thread until it is able to do so.</p>
<p>This function will block the local thread until it is available to acquire
the mutex. Upon returning, the thread is the only thread with the lock
held. An RAII guard is returned to allow scoped unlock of the lock. When
the guard goes out of scope, the mutex will be unlocked.</p>
<p>The exact behavior on locking a mutex in the thread which already holds
the lock is left unspecified. However, this function will not return on
the second call (it might panic or deadlock, for example).</p>
<h1 id="errors" class="section-header"><a href="#errors">Errors</a></h1>
<p>If another user of this mutex panicked while holding the mutex, then
this call will return an error once the mutex is acquired.</p>
<h1 id="panics" class="section-header"><a href="#panics">Panics</a></h1>
<p>This function might panic when called if the lock is already held by
the current thread.</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="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">let</span> <span class="ident">mutex</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="number">0</span>));
<span class="kw">let</span> <span class="ident">c_mutex</span> <span class="op">=</span> <span class="ident">mutex</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-2">*</span><span class="ident">c_mutex</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>() <span class="op">=</span> <span class="number">10</span>;
}).<span class="ident">join</span>().<span class="ident">expect</span>(<span class="string">&quot;thread::spawn failed&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">mutex</span>.<span class="ident">lock</span>().<span class="ident">unwrap</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%3A%7BArc%2C%20Mutex%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20mutex%20%3D%20Arc%3A%3Anew(Mutex%3A%3Anew(0))%3B%0Alet%20c_mutex%20%3D%20mutex.clone()%3B%0A%0Athread%3A%3Aspawn(move%20%7C%7C%20%7B%0A%20%20%20%20*c_mutex.lock().unwrap()%20%3D%2010%3B%0A%7D).join().expect(%22thread%3A%3Aspawn%20failed%22)%3B%0Aassert_eq!(*mutex.lock().unwrap()%2C%2010)%3B%0A%7D">Run</a></pre>
</div><h4 id='method.try_lock' class="method"><span id='try_lock.v' class='invisible'><code>pub fn <a href='#method.try_lock' class='fnname'>try_lock</a>(&amp;self) -&gt; <a class="type" href="../../std/sync/type.TryLockResult.html" title="type std::sync::TryLockResult">TryLockResult</a>&lt;<a class="struct" href="../../std/sync/struct.MutexGuard.html" title="struct std::sync::MutexGuard">MutexGuard</a>&lt;T&gt;&gt;</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/mutex.rs.html#271-279' title='goto source code'>[src]</a></span></h4>
<div class='docblock'><p>Attempts to acquire this lock.</p>
<p>If the lock could not be acquired at this time, then <a href="../../std/result/enum.Result.html#variant.Err"><code>Err</code></a> is returned.
Otherwise, an RAII guard is returned. The lock will be unlocked when the
guard is dropped.</p>
<p>This function does not block.</p>
<h1 id="errors-1" class="section-header"><a href="#errors-1">Errors</a></h1>
<p>If another user of this mutex panicked while holding the mutex, then
this call will return failure if the mutex would otherwise be
acquired.</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="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">let</span> <span class="ident">mutex</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="number">0</span>));
<span class="kw">let</span> <span class="ident">c_mutex</span> <span class="op">=</span> <span class="ident">mutex</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">mut</span> <span class="ident">lock</span> <span class="op">=</span> <span class="ident">c_mutex</span>.<span class="ident">try_lock</span>();
    <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Ok</span>(<span class="kw-2">ref</span> <span class="kw-2">mut</span> <span class="ident">mutex</span>) <span class="op">=</span> <span class="ident">lock</span> {
        <span class="kw-2">*</span><span class="kw-2">*</span><span class="ident">mutex</span> <span class="op">=</span> <span class="number">10</span>;
    } <span class="kw">else</span> {
        <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;try_lock failed&quot;</span>);
    }
}).<span class="ident">join</span>().<span class="ident">expect</span>(<span class="string">&quot;thread::spawn failed&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">mutex</span>.<span class="ident">lock</span>().<span class="ident">unwrap</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%3A%7BArc%2C%20Mutex%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20mutex%20%3D%20Arc%3A%3Anew(Mutex%3A%3Anew(0))%3B%0Alet%20c_mutex%20%3D%20mutex.clone()%3B%0A%0Athread%3A%3Aspawn(move%20%7C%7C%20%7B%0A%20%20%20%20let%20mut%20lock%20%3D%20c_mutex.try_lock()%3B%0A%20%20%20%20if%20let%20Ok(ref%20mut%20mutex)%20%3D%20lock%20%7B%0A%20%20%20%20%20%20%20%20**mutex%20%3D%2010%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20println!(%22try_lock%20failed%22)%3B%0A%20%20%20%20%7D%0A%7D).join().expect(%22thread%3A%3Aspawn%20failed%22)%3B%0Aassert_eq!(*mutex.lock().unwrap()%2C%2010)%3B%0A%7D">Run</a></pre>
</div><h4 id='method.is_poisoned' class="method"><span id='is_poisoned.v' class='invisible'><code>pub fn <a href='#method.is_poisoned' class='fnname'>is_poisoned</a>(&amp;self) -&gt; <a class="primitive" href="../primitive.bool.html">bool</a></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.2.0'>1.2.0</div><a class='srclink' href='../../src/std/sync/mutex.rs.html#304-306' title='goto source code'>[src]</a></span></h4>
<div class='docblock'><p>Determines whether the mutex is poisoned.</p>
<p>If another thread is active, the mutex can still become poisoned at any
time. You should not trust a <code>false</code> value for program correctness
without additional synchronization.</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="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">let</span> <span class="ident">mutex</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="number">0</span>));
<span class="kw">let</span> <span class="ident">c_mutex</span> <span class="op">=</span> <span class="ident">mutex</span>.<span class="ident">clone</span>();

<span class="kw">let</span> _ <span class="op">=</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="ident">_lock</span> <span class="op">=</span> <span class="ident">c_mutex</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
    <span class="macro">panic</span><span class="macro">!</span>(); <span class="comment">// the mutex gets poisoned</span>
}).<span class="ident">join</span>();
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">mutex</span>.<span class="ident">is_poisoned</span>(), <span class="bool-val">true</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%20Mutex%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20mutex%20%3D%20Arc%3A%3Anew(Mutex%3A%3Anew(0))%3B%0Alet%20c_mutex%20%3D%20mutex.clone()%3B%0A%0Alet%20_%20%3D%20thread%3A%3Aspawn(move%20%7C%7C%20%7B%0A%20%20%20%20let%20_lock%20%3D%20c_mutex.lock().unwrap()%3B%0A%20%20%20%20panic!()%3B%20%2F%2F%20the%20mutex%20gets%20poisoned%0A%7D).join()%3B%0Aassert_eq!(mutex.is_poisoned()%2C%20true)%3B%0A%7D">Run</a></pre>
</div><h4 id='method.into_inner' class="method"><span id='into_inner.v' class='invisible'><code>pub fn <a href='#method.into_inner' class='fnname'>into_inner</a>(self) -&gt; <a class="type" href="../../std/sync/type.LockResult.html" title="type std::sync::LockResult">LockResult</a>&lt;T&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,&nbsp;</span></code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.6.0'>1.6.0</div><a class='srclink' href='../../src/std/sync/mutex.rs.html#324-343' title='goto source code'>[src]</a></span></h4>
<div class='docblock'><p>Consumes this mutex, returning the underlying data.</p>
<h1 id="errors-2" class="section-header"><a href="#errors-2">Errors</a></h1>
<p>If another user of this mutex panicked while holding the mutex, then
this call will return an error instead.</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">Mutex</span>;

<span class="kw">let</span> <span class="ident">mutex</span> <span class="op">=</span> <span class="ident">Mutex</span>::<span class="ident">new</span>(<span class="number">0</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">mutex</span>.<span class="ident">into_inner</span>().<span class="ident">unwrap</span>(), <span class="number">0</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%3AMutex%3B%0A%0Alet%20mutex%20%3D%20Mutex%3A%3Anew(0)%3B%0Aassert_eq!(mutex.into_inner().unwrap()%2C%200)%3B%0A%7D">Run</a></pre>
</div><h4 id='method.get_mut' class="method"><span id='get_mut.v' class='invisible'><code>pub fn <a href='#method.get_mut' class='fnname'>get_mut</a>(&amp;mut self) -&gt; <a class="type" href="../../std/sync/type.LockResult.html" title="type std::sync::LockResult">LockResult</a>&lt;<a class="primitive" href="../primitive.reference.html">&amp;mut </a>T&gt;</code></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.6.0'>1.6.0</div><a class='srclink' href='../../src/std/sync/mutex.rs.html#365-370' title='goto source code'>[src]</a></span></h4>
<div class='docblock'><p>Returns a mutable reference to the underlying data.</p>
<p>Since this call borrows the <code>Mutex</code> mutably, no actual locking needs to
take place---the mutable borrow statically guarantees no locks exist.</p>
<h1 id="errors-3" class="section-header"><a href="#errors-3">Errors</a></h1>
<p>If another user of this mutex panicked while holding the mutex, then
this call will return an error instead.</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">Mutex</span>;

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">mutex</span> <span class="op">=</span> <span class="ident">Mutex</span>::<span class="ident">new</span>(<span class="number">0</span>);
<span class="kw-2">*</span><span class="ident">mutex</span>.<span class="ident">get_mut</span>().<span class="ident">unwrap</span>() <span class="op">=</span> <span class="number">10</span>;
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">*</span><span class="ident">mutex</span>.<span class="ident">lock</span>().<span class="ident">unwrap</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%3AMutex%3B%0A%0Alet%20mut%20mutex%20%3D%20Mutex%3A%3Anew(0)%3B%0A*mutex.get_mut().unwrap()%20%3D%2010%3B%0Aassert_eq!(*mutex.lock().unwrap()%2C%2010)%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-UnwindSafe' class='impl'><span class='in-band'><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; <a class="trait" href="../../std/panic/trait.UnwindSafe.html" title="trait std::panic::UnwindSafe">UnwindSafe</a> for <a class="struct" href="../../std/sync/struct.Mutex.html" title="struct std::sync::Mutex">Mutex</a>&lt;T&gt;</code><a href='#impl-UnwindSafe' 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/panic.rs.html#204' title='goto source code'>[src]</a></span></h3>
<div class='impl-items'></div><h3 id='impl-RefUnwindSafe' class='impl'><span class='in-band'><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; <a class="trait" href="../../std/panic/trait.RefUnwindSafe.html" title="trait std::panic::RefUnwindSafe">RefUnwindSafe</a> for <a class="struct" href="../../std/sync/struct.Mutex.html" title="struct std::sync::Mutex">Mutex</a>&lt;T&gt;</code><a href='#impl-RefUnwindSafe' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.12.0'>1.12.0</div><a class='srclink' href='../../src/std/panic.rs.html#228' title='goto source code'>[src]</a></span></h3>
<div class='impl-items'></div><h3 id='impl-Send' class='impl'><span class='in-band'><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a> + <a class="trait" href="../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a>&gt; <a class="trait" href="../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a> for <a class="struct" href="../../std/sync/struct.Mutex.html" title="struct std::sync::Mutex">Mutex</a>&lt;T&gt;</code><a href='#impl-Send' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/mutex.rs.html#135' title='goto source code'>[src]</a></span></h3>
<div class='impl-items'></div><h3 id='impl-Sync' class='impl'><span class='in-band'><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a> + <a class="trait" href="../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a>&gt; <a class="trait" href="../../std/marker/trait.Sync.html" title="trait std::marker::Sync">Sync</a> for <a class="struct" href="../../std/sync/struct.Mutex.html" title="struct std::sync::Mutex">Mutex</a>&lt;T&gt;</code><a href='#impl-Sync' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/mutex.rs.html#137' title='goto source code'>[src]</a></span></h3>
<div class='impl-items'></div><h3 id='impl-Drop' class='impl'><span class='in-band'><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; <a class="trait" href="../../std/ops/trait.Drop.html" title="trait std::ops::Drop">Drop</a> for <a class="struct" href="../../std/sync/struct.Mutex.html" title="struct std::sync::Mutex">Mutex</a>&lt;T&gt;</code><a href='#impl-Drop' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/mutex.rs.html#374-383' 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><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/mutex.rs.html#375-382' title='goto source code'>[src]</a></span></h4>
<div class='docblock'><p>Executes the destructor for this type. <a href="../../std/ops/trait.Drop.html#tymethod.drop">Read more</a></p>
</div></div><h3 id='impl-From%3CT%3E' class='impl'><span class='in-band'><code>impl&lt;T&gt; <a class="trait" href="../../std/convert/trait.From.html" title="trait std::convert::From">From</a>&lt;T&gt; for <a class="struct" href="../../std/sync/struct.Mutex.html" title="struct std::sync::Mutex">Mutex</a>&lt;T&gt;</code><a href='#impl-From%3CT%3E' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><div class='since' title='Stable since Rust version 1.24.0'>1.24.0</div><a class='srclink' href='../../src/std/sync/mutex.rs.html#386-394' title='goto source code'>[src]</a></span></h3>
<div class='impl-items'><h4 id='method.from' class="method"><span id='from.v' class='invisible'><code>fn <a href='../../std/convert/trait.From.html#tymethod.from' class='fnname'>from</a>(t: T) -&gt; Self</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/mutex.rs.html#391-393' title='goto source code'>[src]</a></span></h4>
<div class='docblock'><p>Creates a new mutex in an unlocked state ready for use.
This is equivalent to <a href="#method.new"><code>Mutex::new</code></a>.</p>
</div></div><h3 id='impl-Default' class='impl'><span class='in-band'><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a> + <a class="trait" href="../../std/default/trait.Default.html" title="trait std::default::Default">Default</a>&gt; <a class="trait" href="../../std/default/trait.Default.html" title="trait std::default::Default">Default</a> for <a class="struct" href="../../std/sync/struct.Mutex.html" title="struct std::sync::Mutex">Mutex</a>&lt;T&gt;</code><a href='#impl-Default' class='anchor'></a></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/mutex.rs.html#397-402' 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.Mutex.html" title="struct std::sync::Mutex">Mutex</a>&lt;T&gt;</code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/mutex.rs.html#399-401' title='goto source code'>[src]</a></span></h4>
<div class='docblock'><p>Creates a <code>Mutex&lt;T&gt;</code>, with the <code>Default</code> value for T.</p>
</div></div><h3 id='impl-Debug' class='impl'><span class='in-band'><code>impl&lt;T:&nbsp;?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a> + <a class="trait" href="../../std/fmt/trait.Debug.html" title="trait std::fmt::Debug">Debug</a>&gt; <a class="trait" href="../../std/fmt/trait.Debug.html" title="trait std::fmt::Debug">Debug</a> for <a class="struct" href="../../std/sync/struct.Mutex.html" title="struct std::sync::Mutex">Mutex</a>&lt;T&gt;</code><a href='#impl-Debug' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/mutex.rs.html#405-422' 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><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/std/sync/mutex.rs.html#406-421' 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>&#9166;</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>