<!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 `atomic` mod in crate `core`."> <meta name="keywords" content="rust, rustlang, rust-lang, atomic"> <title>core::sync::atomic - 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 mod"> <!--[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">☰</div> <a href='../../../core/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a> <p class='location'>Module atomic</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#constants">Constants</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'><a href='../../index.html'>core</a>::<wbr><a href='../index.html'>sync</a></p><script>window.sidebarCurrent = {name: 'atomic', ty: 'mod', 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'>Module <a href='../../index.html'>core</a>::<wbr><a href='../index.html'>sync</a>::<wbr><a class="mod" href=''>atomic</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'>−</span>] </a> </span><a class='srclink' href='../../../src/core/sync/atomic.rs.html#11-1886' title='goto source code'>[src]</a></span></h1> <div class='docblock'><p>Atomic types</p> <p>Atomic types provide primitive shared-memory communication between threads, and are the building blocks of other concurrent types.</p> <p>This module defines atomic versions of a select number of primitive types, including <a href="struct.AtomicBool.html"><code>AtomicBool</code></a>, <a href="struct.AtomicIsize.html"><code>AtomicIsize</code></a>, and <a href="struct.AtomicUsize.html"><code>AtomicUsize</code></a>. Atomic types present operations that, when used correctly, synchronize updates between threads.</p> <p>Each method takes an <a href="enum.Ordering.html"><code>Ordering</code></a> which represents the strength of the memory barrier for that operation. These orderings are the same as <a href="http://llvm.org/docs/LangRef.html#memory-model-for-concurrent-operations">LLVM atomic orderings</a>. For more information see the <a href="../../../nomicon/atomics.html">nomicon</a>.</p> <p>Atomic variables are safe to share between threads (they implement <a href="../../marker/trait.Sync.html"><code>Sync</code></a>) but they do not themselves provide the mechanism for sharing and follow the <a href="../../../std/thread/index.html#the-threading-model">threading model</a> of rust. The most common way to share an atomic variable is to put it into an <a href="../../../std/sync/struct.Arc.html"><code>Arc</code></a> (an atomically-reference-counted shared pointer).</p> <p>Most atomic types may be stored in static variables, initialized using the provided static initializers like <a href="constant.ATOMIC_BOOL_INIT.html"><code>ATOMIC_BOOL_INIT</code></a>. Atomic statics are often used for lazy global initialization.</p> <h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1> <p>A simple spinlock:</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="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">atomic</span>::{<span class="ident">AtomicUsize</span>, <span class="ident">Ordering</span>}; <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>; <span class="kw">fn</span> <span class="ident">main</span>() { <span class="kw">let</span> <span class="ident">spinlock</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>(<span class="ident">AtomicUsize</span>::<span class="ident">new</span>(<span class="number">1</span>)); <span class="kw">let</span> <span class="ident">spinlock_clone</span> <span class="op">=</span> <span class="ident">spinlock</span>.<span class="ident">clone</span>(); <span class="kw">let</span> <span class="ident">thread</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="ident">spinlock_clone</span>.<span class="ident">store</span>(<span class="number">0</span>, <span class="ident">Ordering</span>::<span class="ident">SeqCst</span>); }); <span class="comment">// Wait for the other thread to release the lock</span> <span class="kw">while</span> <span class="ident">spinlock</span>.<span class="ident">load</span>(<span class="ident">Ordering</span>::<span class="ident">SeqCst</span>) <span class="op">!=</span> <span class="number">0</span> {} <span class="kw">if</span> <span class="kw">let</span> <span class="prelude-val">Err</span>(<span class="ident">panic</span>) <span class="op">=</span> <span class="ident">thread</span>.<span class="ident">join</span>() { <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Thread had an error: {:?}"</span>, <span class="ident">panic</span>); } }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Ause%20std%3A%3Async%3A%3AArc%3B%0Ause%20std%3A%3Async%3A%3Aatomic%3A%3A%7BAtomicUsize%2C%20Ordering%7D%3B%0Ause%20std%3A%3Athread%3B%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20spinlock%20%3D%20Arc%3A%3Anew(AtomicUsize%3A%3Anew(1))%3B%0A%0A%20%20%20%20let%20spinlock_clone%20%3D%20spinlock.clone()%3B%0A%20%20%20%20let%20thread%20%3D%20thread%3A%3Aspawn(move%7C%7C%20%7B%0A%20%20%20%20%20%20%20%20spinlock_clone.store(0%2C%20Ordering%3A%3ASeqCst)%3B%0A%20%20%20%20%7D)%3B%0A%0A%20%20%20%20%2F%2F%20Wait%20for%20the%20other%20thread%20to%20release%20the%20lock%0A%20%20%20%20while%20spinlock.load(Ordering%3A%3ASeqCst)%20!%3D%200%20%7B%7D%0A%0A%20%20%20%20if%20let%20Err(panic)%20%3D%20thread.join()%20%7B%0A%20%20%20%20%20%20%20%20println!(%22Thread%20had%20an%20error%3A%20%7B%3A%3F%7D%22%2C%20panic)%3B%0A%20%20%20%20%7D%0A%7D%0A">Run</a></pre> <p>Keep a global count of live threads:</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">atomic</span>::{<span class="ident">AtomicUsize</span>, <span class="ident">Ordering</span>, <span class="ident">ATOMIC_USIZE_INIT</span>}; <span class="kw">static</span> <span class="ident">GLOBAL_THREAD_COUNT</span>: <span class="ident">AtomicUsize</span> <span class="op">=</span> <span class="ident">ATOMIC_USIZE_INIT</span>; <span class="kw">let</span> <span class="ident">old_thread_count</span> <span class="op">=</span> <span class="ident">GLOBAL_THREAD_COUNT</span>.<span class="ident">fetch_add</span>(<span class="number">1</span>, <span class="ident">Ordering</span>::<span class="ident">SeqCst</span>); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"live threads: {}"</span>, <span class="ident">old_thread_count</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%3Aatomic%3A%3A%7BAtomicUsize%2C%20Ordering%2C%20ATOMIC_USIZE_INIT%7D%3B%0A%0Astatic%20GLOBAL_THREAD_COUNT%3A%20AtomicUsize%20%3D%20ATOMIC_USIZE_INIT%3B%0A%0Alet%20old_thread_count%20%3D%20GLOBAL_THREAD_COUNT.fetch_add(1%2C%20Ordering%3A%3ASeqCst)%3B%0Aprintln!(%22live%20threads%3A%20%7B%7D%22%2C%20old_thread_count%20%2B%201)%3B%0A%7D">Run</a></pre> </div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2> <table> <tr class=' module-item'> <td><a class="struct" href="struct.AtomicBool.html" title='struct core::sync::atomic::AtomicBool'>AtomicBool</a></td> <td class='docblock-short'> <p>A boolean type which can be safely shared between threads.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.AtomicIsize.html" title='struct core::sync::atomic::AtomicIsize'>AtomicIsize</a></td> <td class='docblock-short'> <p>An integer type which can be safely shared between threads.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.AtomicPtr.html" title='struct core::sync::atomic::AtomicPtr'>AtomicPtr</a></td> <td class='docblock-short'> <p>A raw pointer type which can be safely shared between threads.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.AtomicUsize.html" title='struct core::sync::atomic::AtomicUsize'>AtomicUsize</a></td> <td class='docblock-short'> <p>An integer type which can be safely shared between threads.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="struct" href="struct.AtomicI8.html" title='struct core::sync::atomic::AtomicI8'>AtomicI8</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An integer type which can be safely shared between threads.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="struct" href="struct.AtomicI16.html" title='struct core::sync::atomic::AtomicI16'>AtomicI16</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An integer type which can be safely shared between threads.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="struct" href="struct.AtomicI32.html" title='struct core::sync::atomic::AtomicI32'>AtomicI32</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An integer type which can be safely shared between threads.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="struct" href="struct.AtomicI64.html" title='struct core::sync::atomic::AtomicI64'>AtomicI64</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An integer type which can be safely shared between threads.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="struct" href="struct.AtomicU8.html" title='struct core::sync::atomic::AtomicU8'>AtomicU8</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An integer type which can be safely shared between threads.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="struct" href="struct.AtomicU16.html" title='struct core::sync::atomic::AtomicU16'>AtomicU16</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An integer type which can be safely shared between threads.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="struct" href="struct.AtomicU32.html" title='struct core::sync::atomic::AtomicU32'>AtomicU32</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An integer type which can be safely shared between threads.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="struct" href="struct.AtomicU64.html" title='struct core::sync::atomic::AtomicU64'>AtomicU64</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An integer type which can be safely shared between threads.</p> </td> </tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2> <table> <tr class=' module-item'> <td><a class="enum" href="enum.Ordering.html" title='enum core::sync::atomic::Ordering'>Ordering</a></td> <td class='docblock-short'> <p>Atomic memory orderings</p> </td> </tr></table><h2 id='constants' class='section-header'><a href="#constants">Constants</a></h2> <table> <tr class=' module-item'> <td><a class="constant" href="constant.ATOMIC_BOOL_INIT.html" title='constant core::sync::atomic::ATOMIC_BOOL_INIT'>ATOMIC_BOOL_INIT</a></td> <td class='docblock-short'> <p>An <a href="struct.AtomicBool.html"><code>AtomicBool</code></a> initialized to <code>false</code>.</p> </td> </tr> <tr class=' module-item'> <td><a class="constant" href="constant.ATOMIC_ISIZE_INIT.html" title='constant core::sync::atomic::ATOMIC_ISIZE_INIT'>ATOMIC_ISIZE_INIT</a></td> <td class='docblock-short'> <p>An atomic integer initialized to <code>0</code>.</p> </td> </tr> <tr class=' module-item'> <td><a class="constant" href="constant.ATOMIC_USIZE_INIT.html" title='constant core::sync::atomic::ATOMIC_USIZE_INIT'>ATOMIC_USIZE_INIT</a></td> <td class='docblock-short'> <p>An atomic integer initialized to <code>0</code>.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="constant" href="constant.ATOMIC_I16_INIT.html" title='constant core::sync::atomic::ATOMIC_I16_INIT'>ATOMIC_I16_INIT</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An atomic integer initialized to <code>0</code>.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="constant" href="constant.ATOMIC_I32_INIT.html" title='constant core::sync::atomic::ATOMIC_I32_INIT'>ATOMIC_I32_INIT</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An atomic integer initialized to <code>0</code>.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="constant" href="constant.ATOMIC_I64_INIT.html" title='constant core::sync::atomic::ATOMIC_I64_INIT'>ATOMIC_I64_INIT</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An atomic integer initialized to <code>0</code>.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="constant" href="constant.ATOMIC_I8_INIT.html" title='constant core::sync::atomic::ATOMIC_I8_INIT'>ATOMIC_I8_INIT</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An atomic integer initialized to <code>0</code>.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="constant" href="constant.ATOMIC_U16_INIT.html" title='constant core::sync::atomic::ATOMIC_U16_INIT'>ATOMIC_U16_INIT</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An atomic integer initialized to <code>0</code>.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="constant" href="constant.ATOMIC_U32_INIT.html" title='constant core::sync::atomic::ATOMIC_U32_INIT'>ATOMIC_U32_INIT</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An atomic integer initialized to <code>0</code>.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="constant" href="constant.ATOMIC_U64_INIT.html" title='constant core::sync::atomic::ATOMIC_U64_INIT'>ATOMIC_U64_INIT</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An atomic integer initialized to <code>0</code>.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="constant" href="constant.ATOMIC_U8_INIT.html" title='constant core::sync::atomic::ATOMIC_U8_INIT'>ATOMIC_U8_INIT</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>An atomic integer initialized to <code>0</code>.</p> </td> </tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2> <table> <tr class=' module-item'> <td><a class="fn" href="fn.compiler_fence.html" title='fn core::sync::atomic::compiler_fence'>compiler_fence</a></td> <td class='docblock-short'> <p>A compiler memory fence.</p> </td> </tr> <tr class=' module-item'> <td><a class="fn" href="fn.fence.html" title='fn core::sync::atomic::fence'>fence</a></td> <td class='docblock-short'> <p>An atomic fence.</p> </td> </tr> <tr class=' module-item'> <td><a class="fn" href="fn.spin_loop_hint.html" title='fn core::sync::atomic::spin_loop_hint'>spin_loop_hint</a></td> <td class='docblock-short'> <p>Save power or switch hyperthreads in a busy-wait spin-loop.</p> </td> </tr></table></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>⏎</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 = "core"; </script> <script src="../../../main.js"></script> <script defer src="../../../search-index.js"></script> </body> </html>