Files
phy/rayon/fn.join.html
Orion Kindel 0ce894e6b0 doc
2025-03-18 10:30:23 -05:00

73 lines
8.6 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="Takes two closures and potentially runs them in parallel. It returns a pair of the results from those closures."><title>join in rayon - 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="rayon" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.0 (9fc6b4312 2025-01-07)" data-channel="1.84.0" 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 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" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../rayon/index.html">rayon</a><span class="version">1.10.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">join</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li><li><a href="#warning-about-blocking-io" title="Warning about blocking I/O">Warning about blocking I/O</a></li><li><a href="#panics" title="Panics">Panics</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate rayon</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">rayon</a></span><h1>Function <span class="fn">join</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/rayon_core/join/mod.rs.html#93-98">Source</a> </span></div><pre class="rust item-decl"><code>pub fn join&lt;A, B, RA, RB&gt;(oper_a: A, oper_b: B) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.tuple.html">(RA, RB)</a><div class="where">where
A: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -&gt; RA + <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>,
B: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -&gt; RB + <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>,
RA: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>,
RB: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>,</div></code></pre><details class="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 class="doc-anchor" href="#examples">§</a>Examples</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
// "dividing point" 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 class="doc-anchor" href="#warning-about-blocking-io">§</a>Warning about blocking I/O</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 class="doc-anchor" href="#panics">§</a>Panics</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></body></html>