<!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 `mpsc` mod in crate `std`."> <meta name="keywords" content="rust, rustlang, rust-lang, mpsc"> <title>std::sync::mpsc - 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='../../../std/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 mpsc</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="#functions">Functions</a></li></ul></div><p class='location'><a href='../../index.html'>std</a>::<wbr><a href='../index.html'>sync</a></p><script>window.sidebarCurrent = {name: 'mpsc', 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'>std</a>::<wbr><a href='../index.html'>sync</a>::<wbr><a class="mod" href=''>mpsc</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/std/sync/mpsc/mod.rs.html#11-3102' title='goto source code'>[src]</a></span></h1> <div class='docblock'><p>Multi-producer, single-consumer FIFO queue communication primitives.</p> <p>This module provides message-based communication over channels, concretely defined among three types:</p> <ul> <li><a href="../../../std/sync/mpsc/struct.Sender.html"><code>Sender</code></a></li> <li><a href="../../../std/sync/mpsc/struct.SyncSender.html"><code>SyncSender</code></a></li> <li><a href="../../../std/sync/mpsc/struct.Receiver.html"><code>Receiver</code></a></li> </ul> <p>A <a href="../../../std/sync/mpsc/struct.Sender.html"><code>Sender</code></a> or <a href="../../../std/sync/mpsc/struct.SyncSender.html"><code>SyncSender</code></a> is used to send data to a <a href="../../../std/sync/mpsc/struct.Receiver.html"><code>Receiver</code></a>. Both senders are clone-able (multi-producer) such that many threads can send simultaneously to one receiver (single-consumer).</p> <p>These channels come in two flavors:</p> <ol> <li> <p>An asynchronous, infinitely buffered channel. The <a href="../../../std/sync/mpsc/fn.channel.html"><code>channel</code></a> function will return a <code>(Sender, Receiver)</code> tuple where all sends will be <strong>asynchronous</strong> (they never block). The channel conceptually has an infinite buffer.</p> </li> <li> <p>A synchronous, bounded channel. The <a href="../../../std/sync/mpsc/fn.sync_channel.html"><code>sync_channel</code></a> function will return a <code>(SyncSender, Receiver)</code> tuple where the storage for pending messages is a pre-allocated buffer of a fixed size. All sends will be <strong>synchronous</strong> by blocking until there is buffer space available. Note that a bound of 0 is allowed, causing the channel to become a "rendezvous" channel where each sender atomically hands off a message to a receiver.</p> </li> </ol> <h2 id="disconnection" class="section-header"><a href="#disconnection">Disconnection</a></h2> <p>The send and receive operations on channels will all return a <a href="../../../std/result/enum.Result.html"><code>Result</code></a> indicating whether the operation succeeded or not. An unsuccessful operation is normally indicative of the other half of a channel having "hung up" by being dropped in its corresponding thread.</p> <p>Once half of a channel has been deallocated, most operations can no longer continue to make progress, so <a href="../../../std/result/enum.Result.html#variant.Err"><code>Err</code></a> will be returned. Many applications will continue to <a href="../../../std/result/enum.Result.html#method.unwrap"><code>unwrap</code></a> the results returned from this module, instigating a propagation of failure among threads if one unexpectedly dies.</p> <h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1> <p>Simple usage:</p> <pre class="rust rust-example-rendered"> <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="comment">// Create a simple streaming channel</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="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span><span class="op">||</span> { <span class="ident">tx</span>.<span class="ident">send</span>(<span class="number">10</span>).<span class="ident">unwrap</span>(); }); <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">rx</span>.<span class="ident">recv</span>().<span class="ident">unwrap</span>(), <span class="number">10</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%3Athread%3B%0Ause%20std%3A%3Async%3A%3Ampsc%3A%3Achannel%3B%0A%0A%2F%2F%20Create%20a%20simple%20streaming%20channel%0Alet%20(tx%2C%20rx)%20%3D%20channel()%3B%0Athread%3A%3Aspawn(move%7C%7C%20%7B%0A%20%20%20%20tx.send(10).unwrap()%3B%0A%7D)%3B%0Aassert_eq!(rx.recv().unwrap()%2C%2010)%3B%0A%7D">Run</a></pre> <p>Shared usage:</p> <pre class="rust rust-example-rendered"> <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="comment">// Create a shared channel that can be sent along from many threads</span> <span class="comment">// where tx is the sending half (tx for transmission), and rx is the receiving</span> <span class="comment">// half (rx for receiving).</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="ident">i</span> <span class="kw">in</span> <span class="number">0</span>..<span class="number">10</span> { <span class="kw">let</span> <span class="ident">tx</span> <span class="op">=</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="ident">tx</span>.<span class="ident">send</span>(<span class="ident">i</span>).<span class="ident">unwrap</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">j</span> <span class="op">=</span> <span class="ident">rx</span>.<span class="ident">recv</span>().<span class="ident">unwrap</span>(); <span class="macro">assert</span><span class="macro">!</span>(<span class="number">0</span> <span class="op"><=</span> <span class="ident">j</span> <span class="op">&&</span> <span class="ident">j</span> <span class="op"><</span> <span class="number">10</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%3Athread%3B%0Ause%20std%3A%3Async%3A%3Ampsc%3A%3Achannel%3B%0A%0A%2F%2F%20Create%20a%20shared%20channel%20that%20can%20be%20sent%20along%20from%20many%20threads%0A%2F%2F%20where%20tx%20is%20the%20sending%20half%20(tx%20for%20transmission)%2C%20and%20rx%20is%20the%20receiving%0A%2F%2F%20half%20(rx%20for%20receiving).%0Alet%20(tx%2C%20rx)%20%3D%20channel()%3B%0Afor%20i%20in%200..10%20%7B%0A%20%20%20%20let%20tx%20%3D%20tx.clone()%3B%0A%20%20%20%20thread%3A%3Aspawn(move%7C%7C%20%7B%0A%20%20%20%20%20%20%20%20tx.send(i).unwrap()%3B%0A%20%20%20%20%7D)%3B%0A%7D%0A%0Afor%20_%20in%200..10%20%7B%0A%20%20%20%20let%20j%20%3D%20rx.recv().unwrap()%3B%0A%20%20%20%20assert!(0%20%3C%3D%20j%20%26%26%20j%20%3C%2010)%3B%0A%7D%0A%7D">Run</a></pre> <p>Propagating panics:</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">mpsc</span>::<span class="ident">channel</span>; <span class="comment">// The call to recv() will return an error because the channel has already</span> <span class="comment">// hung up (or been deallocated)</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="op"><</span><span class="ident">i32</span><span class="op">></span>(); <span class="ident">drop</span>(<span class="ident">tx</span>); <span class="macro">assert</span><span class="macro">!</span>(<span class="ident">rx</span>.<span class="ident">recv</span>().<span class="ident">is_err</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%3Ampsc%3A%3Achannel%3B%0A%0A%2F%2F%20The%20call%20to%20recv()%20will%20return%20an%20error%20because%20the%20channel%20has%20already%0A%2F%2F%20hung%20up%20(or%20been%20deallocated)%0Alet%20(tx%2C%20rx)%20%3D%20channel%3A%3A%3Ci32%3E()%3B%0Adrop(tx)%3B%0Aassert!(rx.recv().is_err())%3B%0A%7D">Run</a></pre> <p>Synchronous channels:</p> <pre class="rust rust-example-rendered"> <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">sync_channel</span>; <span class="kw">let</span> (<span class="ident">tx</span>, <span class="ident">rx</span>) <span class="op">=</span> <span class="ident">sync_channel</span>::<span class="op"><</span><span class="ident">i32</span><span class="op">></span>(<span class="number">0</span>); <span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span><span class="op">||</span> { <span class="comment">// This will wait for the parent thread to start receiving</span> <span class="ident">tx</span>.<span class="ident">send</span>(<span class="number">53</span>).<span class="ident">unwrap</span>(); }); <span class="ident">rx</span>.<span class="ident">recv</span>().<span class="ident">unwrap</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%3Athread%3B%0Ause%20std%3A%3Async%3A%3Ampsc%3A%3Async_channel%3B%0A%0Alet%20(tx%2C%20rx)%20%3D%20sync_channel%3A%3A%3Ci32%3E(0)%3B%0Athread%3A%3Aspawn(move%7C%7C%20%7B%0A%20%20%20%20%2F%2F%20This%20will%20wait%20for%20the%20parent%20thread%20to%20start%20receiving%0A%20%20%20%20tx.send(53).unwrap()%3B%0A%7D)%3B%0Arx.recv().unwrap()%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.IntoIter.html" title='struct std::sync::mpsc::IntoIter'>IntoIter</a></td> <td class='docblock-short'> <p>An owning iterator over messages on a <a href="struct.Receiver.html"><code>Receiver</code></a>, created by <strong>Receiver::into_iter</strong>.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.Iter.html" title='struct std::sync::mpsc::Iter'>Iter</a></td> <td class='docblock-short'> <p>An iterator over messages on a <a href="struct.Receiver.html"><code>Receiver</code></a>, created by <a href="struct.Receiver.html#method.iter"><code>iter</code></a>.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.Receiver.html" title='struct std::sync::mpsc::Receiver'>Receiver</a></td> <td class='docblock-short'> <p>The receiving half of Rust's <a href="fn.channel.html"><code>channel</code></a> (or <a href="fn.sync_channel.html"><code>sync_channel</code></a>) type. This half can only be owned by one thread.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.RecvError.html" title='struct std::sync::mpsc::RecvError'>RecvError</a></td> <td class='docblock-short'> <p>An error returned from the <a href="struct.Receiver.html#method.recv"><code>recv</code></a> function on a <a href="struct.Receiver.html"><code>Receiver</code></a>.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.SendError.html" title='struct std::sync::mpsc::SendError'>SendError</a></td> <td class='docblock-short'> <p>An error returned from the <a href="struct.Sender.html#method.send"><code>Sender::send</code></a> or <a href="struct.SyncSender.html#method.send"><code>SyncSender::send</code></a> function on <strong>channel</strong>s.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.Sender.html" title='struct std::sync::mpsc::Sender'>Sender</a></td> <td class='docblock-short'> <p>The sending-half of Rust's asynchronous <a href="fn.channel.html"><code>channel</code></a> type. This half can only be owned by one thread, but it can be cloned to send to other threads.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.SyncSender.html" title='struct std::sync::mpsc::SyncSender'>SyncSender</a></td> <td class='docblock-short'> <p>The sending-half of Rust's synchronous <a href="fn.sync_channel.html"><code>sync_channel</code></a> type.</p> </td> </tr> <tr class=' module-item'> <td><a class="struct" href="struct.TryIter.html" title='struct std::sync::mpsc::TryIter'>TryIter</a></td> <td class='docblock-short'> <p>An iterator that attempts to yield all pending values for a <a href="struct.Receiver.html"><code>Receiver</code></a>, created by <a href="struct.Receiver.html#method.try_iter"><code>try_iter</code></a>.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="struct" href="struct.Handle.html" title='struct std::sync::mpsc::Handle'>Handle</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>A handle to a receiver which is currently a member of a <code>Select</code> set of receivers. This handle is used to keep the receiver in the set as well as interact with the underlying receiver.</p> </td> </tr> <tr class='unstable module-item'> <td><a class="struct" href="struct.Select.html" title='struct std::sync::mpsc::Select'>Select</a></td> <td class='docblock-short'> [<div class='stab unstable'>Experimental</div>] <p>The "receiver set" of the select interface. This structure is used to manage a set of receivers which are being selected over.</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.RecvTimeoutError.html" title='enum std::sync::mpsc::RecvTimeoutError'>RecvTimeoutError</a></td> <td class='docblock-short'> <p>This enumeration is the list of possible errors that made <a href="struct.Receiver.html#method.recv_timeout"><code>recv_timeout</code></a> unable to return data when called. This can occur with both a <a href="fn.channel.html"><code>channel</code></a> and a <a href="fn.sync_channel.html"><code>sync_channel</code></a>.</p> </td> </tr> <tr class=' module-item'> <td><a class="enum" href="enum.TryRecvError.html" title='enum std::sync::mpsc::TryRecvError'>TryRecvError</a></td> <td class='docblock-short'> <p>This enumeration is the list of the possible reasons that <a href="struct.Receiver.html#method.try_recv"><code>try_recv</code></a> could not return data when called. This can occur with both a <a href="fn.channel.html"><code>channel</code></a> and a <a href="fn.sync_channel.html"><code>sync_channel</code></a>.</p> </td> </tr> <tr class=' module-item'> <td><a class="enum" href="enum.TrySendError.html" title='enum std::sync::mpsc::TrySendError'>TrySendError</a></td> <td class='docblock-short'> <p>This enumeration is the list of the possible error outcomes for the <a href="struct.SyncSender.html#method.try_send"><code>try_send</code></a> method.</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.channel.html" title='fn std::sync::mpsc::channel'>channel</a></td> <td class='docblock-short'> <p>Creates a new asynchronous channel, returning the sender/receiver halves. All data sent on the <a href="struct.Sender.html"><code>Sender</code></a> will become available on the <a href="struct.Receiver.html"><code>Receiver</code></a> in the same order as it was sent, and no <a href="struct.Sender.html#method.send"><code>send</code></a> will block the calling thread (this channel has an "infinite buffer", unlike <a href="fn.sync_channel.html"><code>sync_channel</code></a>, which will block after its buffer limit is reached). <a href="struct.Receiver.html#method.recv"><code>recv</code></a> will block until a message is available.</p> </td> </tr> <tr class=' module-item'> <td><a class="fn" href="fn.sync_channel.html" title='fn std::sync::mpsc::sync_channel'>sync_channel</a></td> <td class='docblock-short'> <p>Creates a new synchronous, bounded channel. All data sent on the <a href="struct.SyncSender.html"><code>SyncSender</code></a> will become available on the <a href="struct.Receiver.html"><code>Receiver</code></a> in the same order as it was sent. Like asynchronous <a href="fn.channel.html"><code>channel</code></a>s, the <a href="struct.Receiver.html"><code>Receiver</code></a> will block until a message becomes available. <code>sync_channel</code> differs greatly in the semantics of the sender, however.</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 = "std"; </script> <script src="../../../main.js"></script> <script defer src="../../../search-index.js"></script> </body> </html>