203 lines
No EOL
12 KiB
HTML
203 lines
No EOL
12 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-10223' class='srclink' href='https://doc.rust-lang.org/nightly/std/sync/mutex/struct.Mutex.html?gotosrc=10223' 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></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> |