<!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 `Sync` trait in crate `core`."> <meta name="keywords" content="rust, rustlang, rust-lang, Sync"> <title>core::marker::Sync - Rust</title> <link rel="stylesheet" type="text/css" href="../../normalize.css"> <link rel="stylesheet" type="text/css" href="../../rustdoc.css"> <link rel="stylesheet" type="text/css" href="../../main.css"> <link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"> </head> <body class="rustdoc trait"> <!--[if lte IE 8]> <div class="warning"> This old browser is unsupported and will most likely display funky things. </div> <![endif]--> <nav class="sidebar"> <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'>Trait Sync</p><div class="block items"><ul><li><a href="#implementors">Implementors</a></li></ul></div><p class='location'><a href='../index.html'>core</a>::<wbr><a href='index.html'>marker</a></p><script>window.sidebarCurrent = {name: 'Sync', ty: 'trait', 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"> <h1 class='fqn'><span class='in-band'>Trait <a href='../index.html'>core</a>::<wbr><a href='index.html'>marker</a>::<wbr><a class="trait" href=''>Sync</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/marker.rs.html#347-349' title='goto source code'>[src]</a></span></h1> <pre class='rust trait'><div class="docblock attributes">#[lang = "sync"] </div>pub unsafe trait Sync { }</pre><div class='docblock'><p>Types for which it is safe to share references between threads.</p> <p>This trait is automatically implemented when the compiler determines it's appropriate.</p> <p>The precise definition is: a type <code>T</code> is <code>Sync</code> if <code>&T</code> is <a href="trait.Send.html"><code>Send</code></a>. In other words, if there is no possibility of <a href="../../reference/behavior-considered-undefined.html">undefined behavior</a> (including data races) when passing <code>&T</code> references between threads.</p> <p>As one would expect, primitive types like <a href="../../std/primitive.u8.html"><code>u8</code></a> and <a href="../../std/primitive.f64.html"><code>f64</code></a> are all <code>Sync</code>, and so are simple aggregate types containing them, like tuples, structs and enums. More examples of basic <code>Sync</code> types include "immutable" types like <code>&T</code>, and those with simple inherited mutability, such as <a href="../../std/boxed/struct.Box.html"><code>Box<T></code></a>, <a href="../../std/vec/struct.Vec.html"><code>Vec<T></code></a> and most other collection types. (Generic parameters need to be <code>Sync</code> for their container to be <code>Sync</code>.)</p> <p>A somewhat surprising consequence of the definition is that <code>&mut T</code> is <code>Sync</code> (if <code>T</code> is <code>Sync</code>) even though it seems like that might provide unsynchronized mutation. The trick is that a mutable reference behind a shared reference (that is, <code>& &mut T</code>) becomes read-only, as if it were a <code>& &T</code>. Hence there is no risk of a data race.</p> <p>Types that are not <code>Sync</code> are those that have "interior mutability" in a non-thread-safe form, such as <a href="../cell/struct.Cell.html"><code>cell::Cell</code></a> and <a href="../cell/struct.RefCell.html"><code>cell::RefCell</code></a>. These types allow for mutation of their contents even through an immutable, shared reference. For example the <code>set</code> method on <a href="../cell/struct.Cell.html"><code>Cell<T></code></a> takes <code>&self</code>, so it requires only a shared reference <a href="../cell/struct.Cell.html"><code>&Cell<T></code></a>. The method performs no synchronization, thus <a href="../cell/struct.Cell.html"><code>Cell</code></a> cannot be <code>Sync</code>.</p> <p>Another example of a non-<code>Sync</code> type is the reference-counting pointer <a href="../../std/rc/struct.Rc.html"><code>rc::Rc</code></a>. Given any reference <a href="../../std/rc/struct.Rc.html"><code>&Rc<T></code></a>, you can clone a new <a href="../../std/rc/struct.Rc.html"><code>Rc<T></code></a>, modifying the reference counts in a non-atomic way.</p> <p>For cases when one does need thread-safe interior mutability, Rust provides <a href="../sync/atomic/index.html">atomic data types</a>, as well as explicit locking via <a href="../../std/sync/struct.Mutex.html"><code>sync::Mutex</code></a> and <a href="../../std/sync/struct.RwLock.html"><code>sync::RWLock</code></a>. These types ensure that any mutation cannot cause data races, hence the types are <code>Sync</code>. Likewise, <a href="../../std/sync/struct.Arc.html"><code>sync::Arc</code></a> provides a thread-safe analogue of <a href="../../std/rc/struct.Rc.html"><code>Rc</code></a>.</p> <p>Any types with interior mutability must also use the <a href="../cell/struct.UnsafeCell.html"><code>cell::UnsafeCell</code></a> wrapper around the value(s) which can be mutated through a shared reference. Failing to doing this is <a href="../../reference/behavior-considered-undefined.html">undefined behavior</a>. For example, <a href="../../std/mem/fn.transmute.html"><code>transmute</code></a>-ing from <code>&T</code> to <code>&mut T</code> is invalid.</p> <p>See <a href="../../nomicon/send-and-sync.html">the Nomicon</a> for more details about <code>Sync</code>.</p> </div> <h2 id='implementors'>Implementors</h2> <ul class='item-list' id='implementors-list'> <li><code>impl<T: <a class="trait" href="../../core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> Sync for <a class="struct" href="../../core/ptr/struct.Unique.html" title="struct core::ptr::Unique">Unique</a><T></code></li> <li><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> !Sync for <a class="struct" href="../../core/ptr/struct.Shared.html" title="struct core::ptr::Shared">Shared</a><T></code></li> <li><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> !Sync for *const T</code></li> <li><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> !Sync for *mut T</code></li> <li><code>impl Sync for <a class="struct" href="../../core/sync/atomic/struct.AtomicBool.html" title="struct core::sync::atomic::AtomicBool">AtomicBool</a></code></li> <li><code>impl<T> Sync for <a class="struct" href="../../core/sync/atomic/struct.AtomicPtr.html" title="struct core::sync::atomic::AtomicPtr">AtomicPtr</a><T></code></li> <li><code>impl Sync for <a class="struct" href="../../core/sync/atomic/struct.AtomicI8.html" title="struct core::sync::atomic::AtomicI8">AtomicI8</a></code></li> <li><code>impl Sync for <a class="struct" href="../../core/sync/atomic/struct.AtomicU8.html" title="struct core::sync::atomic::AtomicU8">AtomicU8</a></code></li> <li><code>impl Sync for <a class="struct" href="../../core/sync/atomic/struct.AtomicI16.html" title="struct core::sync::atomic::AtomicI16">AtomicI16</a></code></li> <li><code>impl Sync for <a class="struct" href="../../core/sync/atomic/struct.AtomicU16.html" title="struct core::sync::atomic::AtomicU16">AtomicU16</a></code></li> <li><code>impl Sync for <a class="struct" href="../../core/sync/atomic/struct.AtomicI32.html" title="struct core::sync::atomic::AtomicI32">AtomicI32</a></code></li> <li><code>impl Sync for <a class="struct" href="../../core/sync/atomic/struct.AtomicU32.html" title="struct core::sync::atomic::AtomicU32">AtomicU32</a></code></li> <li><code>impl Sync for <a class="struct" href="../../core/sync/atomic/struct.AtomicI64.html" title="struct core::sync::atomic::AtomicI64">AtomicI64</a></code></li> <li><code>impl Sync for <a class="struct" href="../../core/sync/atomic/struct.AtomicU64.html" title="struct core::sync::atomic::AtomicU64">AtomicU64</a></code></li> <li><code>impl Sync for <a class="struct" href="../../core/sync/atomic/struct.AtomicIsize.html" title="struct core::sync::atomic::AtomicIsize">AtomicIsize</a></code></li> <li><code>impl Sync for <a class="struct" href="../../core/sync/atomic/struct.AtomicUsize.html" title="struct core::sync::atomic::AtomicUsize">AtomicUsize</a></code></li> <li><code>impl<T> !Sync for <a class="struct" href="../../core/cell/struct.Cell.html" title="struct core::cell::Cell">Cell</a><T></code></li> <li><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> !Sync for <a class="struct" href="../../core/cell/struct.RefCell.html" title="struct core::cell::RefCell">RefCell</a><T></code></li> <li><code>impl<T: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> !Sync for <a class="struct" href="../../core/cell/struct.UnsafeCell.html" title="struct core::cell::UnsafeCell">UnsafeCell</a><T></code></li> <li><code>impl<'a, T: <a class="trait" href="../../core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a>> Sync for <a class="struct" href="../../core/slice/struct.Iter.html" title="struct core::slice::Iter">Iter</a><'a, T></code></li> <li><code>impl<'a, T: <a class="trait" href="../../core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a>> Sync for <a class="struct" href="../../core/slice/struct.IterMut.html" title="struct core::slice::IterMut">IterMut</a><'a, T></code></li> </ul><script type="text/javascript" async src="../../implementors/core/marker/trait.Sync.js"> </script></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> <dt>+</dt> <dd>Collapse/expand 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>