261 lines
No EOL
22 KiB
HTML
261 lines
No EOL
22 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<meta name="generator" content="rustdoc">
|
||
<meta name="description" content="API documentation for the Rust `Mutex` struct in crate `bitflags`.">
|
||
<meta name="keywords" content="rust, rustlang, rust-lang, Mutex">
|
||
|
||
<title>bitflags::__core::sync::Mutex - Rust</title>
|
||
|
||
<link rel="stylesheet" type="text/css" href="../../../rustdoc.css">
|
||
<link rel="stylesheet" type="text/css" href="../../../main.css">
|
||
|
||
|
||
|
||
|
||
</head>
|
||
<body class="rustdoc">
|
||
<!--[if lte IE 8]>
|
||
<div class="warning">
|
||
This old browser is unsupported and will most likely display funky
|
||
things.
|
||
</div>
|
||
<![endif]-->
|
||
|
||
|
||
|
||
<nav class="sidebar">
|
||
|
||
<p class='location'><a href='../../index.html'>bitflags</a>::<wbr><a href='../index.html'>__core</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>
|
||
</nav>
|
||
|
||
<nav class="sub">
|
||
<form class="search-form js-only">
|
||
<div class="search-container">
|
||
<input class="search-input" name="search"
|
||
autocomplete="off"
|
||
placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
|
||
type="search">
|
||
</div>
|
||
</form>
|
||
</nav>
|
||
|
||
<section id='main' class="content struct">
|
||
<h1 class='fqn'><span class='in-band'>Struct <a href='../../index.html'>bitflags</a>::<wbr><a href='../index.html'>__core</a>::<wbr><a href='index.html'>sync</a>::<wbr><a class='struct' href=''>Mutex</a></span><span class='out-of-band'><span id='render-detail'>
|
||
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
|
||
[<span class='inner'>−</span>]
|
||
</a>
|
||
</span><a id='src-10101' class='srclink' href='https://doc.rust-lang.org/nightly/std/sync/mutex/struct.Mutex.html?gotosrc=10101' title='goto source code'>[src]</a></span></h1>
|
||
<pre class='rust struct'>pub struct Mutex<T> <span class='where'>where T: ?<a class='trait' href='../../../bitflags/__core/marker/trait.Sized.html' title='bitflags::__core::marker::Sized'>Sized</a></span> {
|
||
// some fields omitted
|
||
}</pre><span class="since">1.0.0</span><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 <code>new</code>
|
||
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 <code>lock</code> and <code>try_lock</code>, 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 "poisoning" where a
|
||
mutex is considered poisoned whenever a thread panics while holding the
|
||
lock. 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 <code>lock</code> and <code>try_lock</code> methods return a
|
||
<code>Result</code> which indicates whether a mutex has been poisoned or not. Most
|
||
usage of a mutex will simply <code>unwrap()</code> 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 <code>PoisonError</code> type has an <code>into_inner</code> 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'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='number'>10</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'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='op'>*</span><span class='ident'>data</span> <span class='op'>+=</span> <span class='number'>1</span>;
|
||
<span class='kw'>if</span> <span class='op'>*</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>();</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'>-></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'>=></span> <span class='ident'>guard</span>,
|
||
<span class='prelude-val'>Err</span>(<span class='ident'>poisoned</span>) <span class='op'>=></span> <span class='ident'>poisoned</span>.<span class='ident'>into_inner</span>(),
|
||
};
|
||
|
||
<span class='op'>*</span><span class='ident'>guard</span> <span class='op'>+=</span> <span class='number'>1</span>;</pre>
|
||
</div><h2 id='methods'>Methods</h2><h3 class='impl'><code>impl<T> <a class='struct' href='../../../bitflags/__core/sync/struct.Mutex.html' title='bitflags::__core::sync::Mutex'>Mutex</a><T></code></h3><div class='impl-items'><h4 id='method.new' class='method'><code>fn <a href='#method.new' class='fnname'>new</a>(t: T) -> <a class='struct' href='../../../bitflags/__core/sync/struct.Mutex.html' title='bitflags::__core::sync::Mutex'>Mutex</a><T></code></h4>
|
||
<div class='docblock'><p>Creates a new mutex in an unlocked state ready for use.</p>
|
||
</div></div><h3 class='impl'><code>impl<T> <a class='struct' href='../../../bitflags/__core/sync/struct.Mutex.html' title='bitflags::__core::sync::Mutex'>Mutex</a><T> <span class='where'>where T: ?<a class='trait' href='../../../bitflags/__core/marker/trait.Sized.html' title='bitflags::__core::marker::Sized'>Sized</a></span></code></h3><div class='impl-items'><h4 id='method.lock' class='method'><code>fn <a href='#method.lock' class='fnname'>lock</a>(&self) -> <a class='enum' href='../../../bitflags/__core/result/enum.Result.html' title='bitflags::__core::result::Result'>Result</a><<a class='struct' href='../../../bitflags/__core/sync/struct.MutexGuard.html' title='bitflags::__core::sync::MutexGuard'>MutexGuard</a><T>, <a class='struct' href='../../../bitflags/__core/sync/struct.PoisonError.html' title='bitflags::__core::sync::PoisonError'>PoisonError</a><<a class='struct' href='../../../bitflags/__core/sync/struct.MutexGuard.html' title='bitflags::__core::sync::MutexGuard'>MutexGuard</a><T>>></code></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 mutex
|
||
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>
|
||
</div><h4 id='method.try_lock' class='method'><code>fn <a href='#method.try_lock' class='fnname'>try_lock</a>(&self) -> <a class='enum' href='../../../bitflags/__core/result/enum.Result.html' title='bitflags::__core::result::Result'>Result</a><<a class='struct' href='../../../bitflags/__core/sync/struct.MutexGuard.html' title='bitflags::__core::sync::MutexGuard'>MutexGuard</a><T>, <a class='enum' href='../../../bitflags/__core/sync/enum.TryLockError.html' title='bitflags::__core::sync::TryLockError'>TryLockError</a><<a class='struct' href='../../../bitflags/__core/sync/struct.MutexGuard.html' title='bitflags::__core::sync::MutexGuard'>MutexGuard</a><T>>></code></h4>
|
||
<div class='docblock'><p>Attempts to acquire this lock.</p>
|
||
|
||
<p>If the lock could not be acquired at this time, then <code>Err</code> 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>
|
||
</div><h4 id='method.is_poisoned' class='method'><code>fn <a href='#method.is_poisoned' class='fnname'>is_poisoned</a>(&self) -> <a class='primitive' href='https://doc.rust-lang.org/nightly/bitflags/primitive.bool.html'>bool</a></code><span class="since">1.2.0</span></h4>
|
||
<div class='docblock'><p>Determines whether the lock is poisoned.</p>
|
||
|
||
<p>If another thread is active, the lock can still become poisoned at any
|
||
time. You should not trust a <code>false</code> value for program correctness
|
||
without additional synchronization.</p>
|
||
</div><h4 id='method.into_inner' class='method'><code>fn <a href='#method.into_inner' class='fnname'>into_inner</a>(self) -> <a class='enum' href='../../../bitflags/__core/result/enum.Result.html' title='bitflags::__core::result::Result'>Result</a><T, <a class='struct' href='../../../bitflags/__core/sync/struct.PoisonError.html' title='bitflags::__core::sync::PoisonError'>PoisonError</a><T>></code><span class="since">1.6.0</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>
|
||
</div><h4 id='method.get_mut' class='method'><code>fn <a href='#method.get_mut' class='fnname'>get_mut</a>(&mut self) -> <a class='enum' href='../../../bitflags/__core/result/enum.Result.html' title='bitflags::__core::result::Result'>Result</a><&mut T, <a class='struct' href='../../../bitflags/__core/sync/struct.PoisonError.html' title='bitflags::__core::sync::PoisonError'>PoisonError</a><&mut T>></code><span class="since">1.6.0</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>
|
||
</div></div><h2 id='implementations'>Trait Implementations</h2><h3 class='impl'><code>impl<T> <a class='trait' href='../../../bitflags/__core/panic/trait.UnwindSafe.html' title='bitflags::__core::panic::UnwindSafe'>UnwindSafe</a> for <a class='struct' href='../../../bitflags/__core/sync/struct.Mutex.html' title='bitflags::__core::sync::Mutex'>Mutex</a><T> <span class='where'>where T: ?<a class='trait' href='../../../bitflags/__core/marker/trait.Sized.html' title='bitflags::__core::marker::Sized'>Sized</a></span></code><span class="since">1.9.0</span></h3><div class='impl-items'></div><h3 class='impl'><code>impl<T> <a class='trait' href='../../../bitflags/__core/marker/trait.Send.html' title='bitflags::__core::marker::Send'>Send</a> for <a class='struct' href='../../../bitflags/__core/sync/struct.Mutex.html' title='bitflags::__core::sync::Mutex'>Mutex</a><T> <span class='where'>where T: <a class='trait' href='../../../bitflags/__core/marker/trait.Send.html' title='bitflags::__core::marker::Send'>Send</a> + ?<a class='trait' href='../../../bitflags/__core/marker/trait.Sized.html' title='bitflags::__core::marker::Sized'>Sized</a></span></code></h3><div class='impl-items'></div><h3 class='impl'><code>impl<T> <a class='trait' href='../../../bitflags/__core/marker/trait.Sync.html' title='bitflags::__core::marker::Sync'>Sync</a> for <a class='struct' href='../../../bitflags/__core/sync/struct.Mutex.html' title='bitflags::__core::sync::Mutex'>Mutex</a><T> <span class='where'>where T: <a class='trait' href='../../../bitflags/__core/marker/trait.Send.html' title='bitflags::__core::marker::Send'>Send</a> + ?<a class='trait' href='../../../bitflags/__core/marker/trait.Sized.html' title='bitflags::__core::marker::Sized'>Sized</a></span></code></h3><div class='impl-items'></div><h3 class='impl'><code>impl<T> <a class='trait' href='../../../bitflags/__core/ops/trait.Drop.html' title='bitflags::__core::ops::Drop'>Drop</a> for <a class='struct' href='../../../bitflags/__core/sync/struct.Mutex.html' title='bitflags::__core::sync::Mutex'>Mutex</a><T> <span class='where'>where T: ?<a class='trait' href='../../../bitflags/__core/marker/trait.Sized.html' title='bitflags::__core::marker::Sized'>Sized</a></span></code></h3><div class='impl-items'><h4 id='method.drop' class='method'><code>fn <a href='../../../bitflags/__core/ops/trait.Drop.html#tymethod.drop' class='fnname'>drop</a>(&mut self)</code></h4>
|
||
</div><h3 class='impl'><code>impl<T> <a class='trait' href='../../../bitflags/__core/default/trait.Default.html' title='bitflags::__core::default::Default'>Default</a> for <a class='struct' href='../../../bitflags/__core/sync/struct.Mutex.html' title='bitflags::__core::sync::Mutex'>Mutex</a><T> <span class='where'>where T: <a class='trait' href='../../../bitflags/__core/default/trait.Default.html' title='bitflags::__core::default::Default'>Default</a> + ?<a class='trait' href='../../../bitflags/__core/marker/trait.Sized.html' title='bitflags::__core::marker::Sized'>Sized</a></span></code><span class="since">1.9.0</span></h3><div class='impl-items'><h4 id='method.default' class='method'><code>fn <a href='../../../bitflags/__core/default/trait.Default.html#tymethod.default' class='fnname'>default</a>() -> <a class='struct' href='../../../bitflags/__core/sync/struct.Mutex.html' title='bitflags::__core::sync::Mutex'>Mutex</a><T></code></h4>
|
||
</div><h3 class='impl'><code>impl<T> <a class='trait' href='../../../bitflags/__core/fmt/trait.Debug.html' title='bitflags::__core::fmt::Debug'>Debug</a> for <a class='struct' href='../../../bitflags/__core/sync/struct.Mutex.html' title='bitflags::__core::sync::Mutex'>Mutex</a><T> <span class='where'>where T: <a class='trait' href='../../../bitflags/__core/fmt/trait.Debug.html' title='bitflags::__core::fmt::Debug'>Debug</a> + ?<a class='trait' href='../../../bitflags/__core/marker/trait.Sized.html' title='bitflags::__core::marker::Sized'>Sized</a></span></code></h3><div class='impl-items'><h4 id='method.fmt' class='method'><code>fn <a href='../../../bitflags/__core/fmt/trait.Debug.html#tymethod.fmt' class='fnname'>fmt</a>(&self, f: &mut <a class='struct' href='../../../bitflags/__core/fmt/struct.Formatter.html' title='bitflags::__core::fmt::Formatter'>Formatter</a>) -> <a class='enum' href='../../../bitflags/__core/result/enum.Result.html' title='bitflags::__core::result::Result'>Result</a><<a class='primitive' href='https://doc.rust-lang.org/nightly/bitflags/primitive.tuple.html'>()</a>, <a class='struct' href='../../../bitflags/__core/fmt/struct.Error.html' title='bitflags::__core::fmt::Error'>Error</a>></code></h4>
|
||
</div></section>
|
||
<section id='search' class="content hidden"></section>
|
||
|
||
<section class="footer"></section>
|
||
|
||
<aside id="help" class="hidden">
|
||
<div>
|
||
<h1 class="hidden">Help</h1>
|
||
|
||
<div class="shortcuts">
|
||
<h2>Keyboard Shortcuts</h2>
|
||
|
||
<dl>
|
||
<dt>?</dt>
|
||
<dd>Show this help dialog</dd>
|
||
<dt>S</dt>
|
||
<dd>Focus the search field</dd>
|
||
<dt>⇤</dt>
|
||
<dd>Move up in search results</dd>
|
||
<dt>⇥</dt>
|
||
<dd>Move down in search results</dd>
|
||
<dt>⏎</dt>
|
||
<dd>Go to active search result</dd>
|
||
</dl>
|
||
</div>
|
||
|
||
<div class="infos">
|
||
<h2>Search Tricks</h2>
|
||
|
||
<p>
|
||
Prefix searches with a type followed by a colon (e.g.
|
||
<code>fn:</code>) to restrict the search to a given type.
|
||
</p>
|
||
|
||
<p>
|
||
Accepted types are: <code>fn</code>, <code>mod</code>,
|
||
<code>struct</code>, <code>enum</code>,
|
||
<code>trait</code>, <code>type</code>, <code>macro</code>,
|
||
and <code>const</code>.
|
||
</p>
|
||
|
||
<p>
|
||
Search functions by type signature (e.g.
|
||
<code>vec -> usize</code> or <code>* -> vec</code>)
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</aside>
|
||
|
||
|
||
|
||
<script>
|
||
window.rootPath = "../../../";
|
||
window.currentCrate = "bitflags";
|
||
window.playgroundUrl = "";
|
||
</script>
|
||
<script src="../../../jquery.js"></script>
|
||
<script src="../../../main.js"></script>
|
||
|
||
<script defer src="../../../search-index.js"></script>
|
||
</body>
|
||
</html> |