blob: afdf7fdaa7ac4c67fdc2baec95cf2443af029e70 [file] [log] [blame]
<!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="Takes two closures and potentially runs them in parallel. It returns a pair of the results from those closures."><meta name="keywords" content="rust, rustlang, rust-lang, join"><title>join in rayon - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc fn"><!--[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">&#9776;</button><a class="sidebar-logo" href="../rayon/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../rayon/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><div class="sidebar-elems"><h2><a href="index.html">In rayon</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Function <a href="index.html">rayon</a>::<wbr><a class="fn" href="#">join</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/rayon_core/join/mod.rs.html#93-98">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust fn"><code>pub fn join&lt;A, B, RA, RB&gt;(oper_a: A, oper_b: B) -&gt; (RA, RB)<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;A: FnOnce() -&gt; RA + Send,<br>&nbsp;&nbsp;&nbsp;&nbsp;B: FnOnce() -&gt; RB + Send,<br>&nbsp;&nbsp;&nbsp;&nbsp;RA: Send,<br>&nbsp;&nbsp;&nbsp;&nbsp;RB: Send,</span></code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Takes two closures and <em>potentially</em> runs them in parallel. It
returns a pair of the results from those closures.</p>
<p>Conceptually, calling <code>join()</code> is similar to spawning two threads,
one executing each of the two closures. However, the
implementation is quite different and incurs very low
overhead. The underlying technique is called “work stealing”: the
Rayon runtime uses a fixed pool of worker threads and attempts to
only execute code in parallel when there are idle CPUs to handle
it.</p>
<p>When <code>join</code> is called from outside the thread pool, the calling
thread will block while the closures execute in the pool. When
<code>join</code> is called within the pool, the calling thread still actively
participates in the thread pool. It will begin by executing closure
A (on the current thread). While it is doing that, it will advertise
closure B as being available for other threads to execute. Once closure A
has completed, the current thread will try to execute closure B;
if however closure B has been stolen, then it will look for other work
while waiting for the thief to fully execute closure B. (This is the
typical work-stealing strategy).</p>
<h2 id="examples"><a href="#examples">Examples</a></h2>
<p>This example uses join to perform a quick-sort (note this is not a
particularly optimized implementation: if you <strong>actually</strong> want to
sort for real, you should prefer <a href="../rayon/slice/trait.ParallelSliceMut.html#method.par_sort">the <code>par_sort</code> method</a> offered
by Rayon).</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>v = <span class="macro">vec!</span>[<span class="number">5</span>, <span class="number">1</span>, <span class="number">8</span>, <span class="number">22</span>, <span class="number">0</span>, <span class="number">44</span>];
quick_sort(<span class="kw-2">&amp;mut </span>v);
<span class="macro">assert_eq!</span>(v, <span class="macro">vec!</span>[<span class="number">0</span>, <span class="number">1</span>, <span class="number">5</span>, <span class="number">8</span>, <span class="number">22</span>, <span class="number">44</span>]);
<span class="kw">fn </span>quick_sort&lt;T:PartialOrd+Send&gt;(v: <span class="kw-2">&amp;mut </span>[T]) {
<span class="kw">if </span>v.len() &gt; <span class="number">1 </span>{
<span class="kw">let </span>mid = partition(v);
<span class="kw">let </span>(lo, hi) = v.split_at_mut(mid);
rayon::join(|| quick_sort(lo),
|| quick_sort(hi));
}
}
<span class="comment">// Partition rearranges all items `&lt;=` to the pivot
// item (arbitrary selected to be the last item in the slice)
// to the first half of the slice. It then returns the
// &quot;dividing point&quot; where the pivot is placed.
</span><span class="kw">fn </span>partition&lt;T:PartialOrd+Send&gt;(v: <span class="kw-2">&amp;mut </span>[T]) -&gt; usize {
<span class="kw">let </span>pivot = v.len() - <span class="number">1</span>;
<span class="kw">let </span><span class="kw-2">mut </span>i = <span class="number">0</span>;
<span class="kw">for </span>j <span class="kw">in </span><span class="number">0</span>..pivot {
<span class="kw">if </span>v[j] &lt;= v[pivot] {
v.swap(i, j);
i += <span class="number">1</span>;
}
}
v.swap(i, pivot);
i
}</code></pre></div>
<h2 id="warning-about-blocking-io"><a href="#warning-about-blocking-io">Warning about blocking I/O</a></h2>
<p>The assumption is that the closures given to <code>join()</code> are
CPU-bound tasks that do not perform I/O or other blocking
operations. If you do perform I/O, and that I/O should block
(e.g., waiting for a network request), the overall performance may
be poor. Moreover, if you cause one closure to be blocked waiting
on another (for example, using a channel), that could lead to a
deadlock.</p>
<h2 id="panics"><a href="#panics">Panics</a></h2>
<p>No matter what happens, both closures will always be executed. If
a single closure panics, whether it be the first or second
closure, that panic will be propagated and hence <code>join()</code> will
panic with the same panic value. If both closures panic, <code>join()</code>
will panic with the panic value from the first closure.</p>
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="rayon" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>