mpvrc/parking_lot/type.FairMutex.html

60 lines
No EOL
7.4 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="A mutual exclusive primitive that is always fair, useful for protecting shared data"><title>FairMutex in parking_lot - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="parking_lot" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.1 (e71f9a9a9 2025-01-27)" data-channel="1.84.1" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc type"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../parking_lot/index.html">parking_<wbr>lot</a><span class="version">0.12.3</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Fair<wbr>Mutex</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#differences-from-the-standard-library-mutex" title="Differences from the standard library `Mutex`">Differences from the standard library <code>Mutex</code></a></li><li><a href="#examples" title="Examples">Examples</a></li></ul><h3><a href="#aliased-type">Aliased type</a></h3></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate parking_<wbr>lot</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="index.html">parking_lot</a></span><h1>Type Alias <span class="type">FairMutex</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/parking_lot/fair_mutex.rs.html#77">Source</a> </span></div><pre class="rust item-decl"><code>pub type FairMutex&lt;T&gt; = <a class="struct" href="../lock_api/mutex/struct.Mutex.html" title="struct lock_api::mutex::Mutex">Mutex</a>&lt;<a class="struct" href="struct.RawFairMutex.html" title="struct parking_lot::RawFairMutex">RawFairMutex</a>, T&gt;;</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A mutual exclusive primitive that is always fair, useful for protecting shared data</p>
<p>This mutex will block threads waiting for the lock to become available. The
mutex can be statically initialized or created by the <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>
<p>The regular mutex provided by <code>parking_lot</code> uses eventual fairness
(after some time it will default to the fair algorithm), but eventual
fairness does not provide the same guarantees an always fair method would.
Fair mutexes are generally slower, but sometimes needed.</p>
<p>In a fair mutex the waiters form a queue, and the lock is always granted to
the next requester in the queue, in first-in first-out order. This ensures
that one thread cannot starve others by quickly re-acquiring the lock after
releasing it.</p>
<p>A fair mutex may not be interesting if threads have different priorities (this is known as
priority inversion).</p>
<h2 id="differences-from-the-standard-library-mutex"><a class="doc-anchor" href="#differences-from-the-standard-library-mutex">§</a>Differences from the standard library <code>Mutex</code></h2>
<ul>
<li>No poisoning, the lock is released normally on panic.</li>
<li>Only requires 1 byte of space, whereas the standard library boxes the
<code>FairMutex</code> due to platform limitations.</li>
<li>Can be statically constructed.</li>
<li>Does not require any drop glue when dropped.</li>
<li>Inline fast path for the uncontended case.</li>
<li>Efficient handling of micro-contention using adaptive spinning.</li>
<li>Allows raw locking &amp; unlocking without a guard.</li>
</ul>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>parking_lot::FairMutex;
<span class="kw">use </span>std::sync::{Arc, mpsc::channel};
<span class="kw">use </span>std::thread;
<span class="kw">const </span>N: usize = <span class="number">10</span>;
<span class="comment">// Spawn a few threads to increment a shared variable (non-atomically), and
// let the main thread know once all increments are done.
//
// Here we're using an Arc to share memory among threads, and the data inside
// the Arc is protected with a mutex.
</span><span class="kw">let </span>data = Arc::new(FairMutex::new(<span class="number">0</span>));
<span class="kw">let </span>(tx, rx) = channel();
<span class="kw">for _ in </span><span class="number">0</span>..<span class="number">10 </span>{
<span class="kw">let </span>(data, tx) = (Arc::clone(<span class="kw-2">&amp;</span>data), tx.clone());
thread::spawn(<span class="kw">move </span>|| {
<span class="comment">// The shared state can only be accessed once the lock is held.
// Our non-atomic increment is safe because we're the only thread
// which can access the shared state when the lock is held.
</span><span class="kw">let </span><span class="kw-2">mut </span>data = data.lock();
<span class="kw-2">*</span>data += <span class="number">1</span>;
<span class="kw">if </span><span class="kw-2">*</span>data == N {
tx.send(()).unwrap();
}
<span class="comment">// the lock is unlocked here when `data` goes out of scope.
</span>});
}
rx.recv().unwrap();</code></pre></div>
</div></details><h2 id="aliased-type" class="section-header">Aliased Type<a href="#aliased-type" class="anchor">§</a></h2><pre class="rust item-decl"><code>struct FairMutex&lt;T&gt; { <span class="comment">/* private fields */</span> }</code></pre><script src="../type.impl/lock_api/mutex/struct.Mutex.js" data-self-path="parking_lot::fair_mutex::FairMutex" async></script></section></div></main></body></html>