blob: 1a0ff26f91fa28ad32699dd58a43134891ff0fa4 [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="Used to create a new `ThreadPool` or to configure the global rayon thread pool."><meta name="keywords" content="rust, rustlang, rust-lang, ThreadPoolBuilder"><title>ThreadPoolBuilder 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 struct"><!--[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><h2 class="location"><a href="#">ThreadPoolBuilder</a></h2><div class="sidebar-elems"><section><h3><a href="#implementations">Methods</a></h3><ul class="block"><li><a href="#method.breadth_first">breadth_first</a></li><li><a href="#method.build">build</a></li><li><a href="#method.build_global">build_global</a></li><li><a href="#method.build_scoped">build_scoped</a></li><li><a href="#method.exit_handler">exit_handler</a></li><li><a href="#method.new">new</a></li><li><a href="#method.num_threads">num_threads</a></li><li><a href="#method.panic_handler">panic_handler</a></li><li><a href="#method.spawn_handler">spawn_handler</a></li><li><a href="#method.stack_size">stack_size</a></li><li><a href="#method.start_handler">start_handler</a></li><li><a href="#method.thread_name">thread_name</a></li></ul><h3><a href="#trait-implementations">Trait Implementations</a></h3><ul class="block"><li><a href="#impl-Debug-for-ThreadPoolBuilder%3CS%3E">Debug</a></li><li><a href="#impl-Default-for-ThreadPoolBuilder%3CDefaultSpawn%3E">Default</a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block"><li><a href="#impl-RefUnwindSafe-for-ThreadPoolBuilder%3CS%3E">!RefUnwindSafe</a></li><li><a href="#impl-Send-for-ThreadPoolBuilder%3CS%3E">!Send</a></li><li><a href="#impl-Sync-for-ThreadPoolBuilder%3CS%3E">!Sync</a></li><li><a href="#impl-Unpin-for-ThreadPoolBuilder%3CS%3E">Unpin</a></li><li><a href="#impl-UnwindSafe-for-ThreadPoolBuilder%3CS%3E">!UnwindSafe</a></li></ul><h3><a href="#blanket-implementations">Blanket Implementations</a></h3><ul class="block"><li><a href="#impl-Any-for-ThreadPoolBuilder%3CS%3E">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-ThreadPoolBuilder%3CS%3E">Borrow&lt;T&gt;</a></li><li><a href="#impl-BorrowMut%3CT%3E-for-ThreadPoolBuilder%3CS%3E">BorrowMut&lt;T&gt;</a></li><li><a href="#impl-From%3CT%3E-for-ThreadPoolBuilder%3CS%3E">From&lt;T&gt;</a></li><li><a href="#impl-Into%3CU%3E-for-ThreadPoolBuilder%3CS%3E">Into&lt;U&gt;</a></li><li><a href="#impl-Pointable-for-ThreadPoolBuilder%3CS%3E">Pointable</a></li><li><a href="#impl-TryFrom%3CU%3E-for-ThreadPoolBuilder%3CS%3E">TryFrom&lt;U&gt;</a></li><li><a href="#impl-TryInto%3CU%3E-for-ThreadPoolBuilder%3CS%3E">TryInto&lt;U&gt;</a></li></ul></section><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">Struct <a href="index.html">rayon</a>::<wbr><a class="struct" href="#">ThreadPoolBuilder</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/lib.rs.html#172">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 struct"><code>pub struct ThreadPoolBuilder&lt;S&nbsp;=&nbsp;DefaultSpawn&gt; { /* private fields */ }</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Used to create a new <a href="struct.ThreadPool.html"><code>ThreadPool</code></a> or to configure the global rayon thread pool.</p>
<h3 id="creating-a-threadpool"><a href="#creating-a-threadpool">Creating a ThreadPool</a></h3>
<p>The following creates a thread pool with 22 threads.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span>pool = rayon::ThreadPoolBuilder::new().num_threads(<span class="number">22</span>).build().unwrap();</code></pre></div>
<p>To instead configure the global thread pool, use <a href="struct.ThreadPoolBuilder.html#method.build_global"><code>build_global()</code></a>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code>rayon::ThreadPoolBuilder::new().num_threads(<span class="number">22</span>).build_global().unwrap();</code></pre></div>
</div></details><h2 id="implementations" class="small-section-header">Implementations<a href="#implementations" class="anchor"></a></h2><div id="implementations-list"><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-ThreadPoolBuilder%3CDefaultSpawn%3E" class="impl has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#242">source</a><a href="#impl-ThreadPoolBuilder%3CDefaultSpawn%3E" class="anchor"></a><h3 class="code-header">impl <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;DefaultSpawn&gt;</h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.new" class="method has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#244">source</a><h4 class="code-header">pub fn <a href="#method.new" class="fnname">new</a>() -&gt; <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;DefaultSpawn&gt;</h4></section></summary><div class="docblock"><p>Creates and returns a valid rayon thread pool builder, but does not initialize it.</p>
</div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#251">source</a><a href="#impl-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;S&gt; <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;S: ThreadSpawn,</span></h3></section></summary><div class="docblock"><p>Note: the <code>S: ThreadSpawn</code> constraint is an internal implementation detail for the
default spawn and those set by <a href="#method.spawn_handler"><code>spawn_handler</code></a>.</p>
</div><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.build" class="method has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#256">source</a><h4 class="code-header">pub fn <a href="#method.build" class="fnname">build</a>(self) -&gt; Result&lt;<a class="struct" href="struct.ThreadPool.html" title="struct rayon::ThreadPool">ThreadPool</a>, <a class="struct" href="struct.ThreadPoolBuildError.html" title="struct rayon::ThreadPoolBuildError">ThreadPoolBuildError</a>&gt;</h4></section></summary><div class="docblock"><p>Creates a new <code>ThreadPool</code> initialized using this configuration.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.build_global" class="method has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#277">source</a><h4 class="code-header">pub fn <a href="#method.build_global" class="fnname">build_global</a>(self) -&gt; Result&lt;(), <a class="struct" href="struct.ThreadPoolBuildError.html" title="struct rayon::ThreadPoolBuildError">ThreadPoolBuildError</a>&gt;</h4></section></summary><div class="docblock"><p>Initializes the global thread pool. This initialization is
<strong>optional</strong>. If you do not call this function, the thread pool
will be automatically initialized with the default
configuration. Calling <code>build_global</code> is not recommended, except
in two scenarios:</p>
<ul>
<li>You wish to change the default configuration.</li>
<li>You are running a benchmark, in which case initializing may
yield slightly more consistent results, since the worker threads
will already be ready to go even in the first iteration. But
this cost is minimal.</li>
</ul>
<p>Initialization of the global thread pool happens exactly
once. Once started, the configuration cannot be
changed. Therefore, if you call <code>build_global</code> a second time, it
will return an error. An <code>Ok</code> result indicates that this
is the first initialization of the thread pool.</p>
</div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-ThreadPoolBuilder%3CDefaultSpawn%3E-1" class="impl has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#284">source</a><a href="#impl-ThreadPoolBuilder%3CDefaultSpawn%3E-1" class="anchor"></a><h3 class="code-header">impl <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;DefaultSpawn&gt;</h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.build_scoped" class="method has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#322-325">source</a><h4 class="code-header">pub fn <a href="#method.build_scoped" class="fnname">build_scoped</a>&lt;W, F, R&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;self,<br>&nbsp;&nbsp;&nbsp;&nbsp;wrapper: W,<br>&nbsp;&nbsp;&nbsp;&nbsp;with_pool: F<br>) -&gt; Result&lt;R, <a class="struct" href="struct.ThreadPoolBuildError.html" title="struct rayon::ThreadPoolBuildError">ThreadPoolBuildError</a>&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;W: Fn(<a class="struct" href="struct.ThreadBuilder.html" title="struct rayon::ThreadBuilder">ThreadBuilder</a>) + Sync,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: FnOnce(&amp;<a class="struct" href="struct.ThreadPool.html" title="struct rayon::ThreadPool">ThreadPool</a>) -&gt; R,</span></h4></section></summary><div class="docblock"><p>Creates a scoped <code>ThreadPool</code> initialized using this configuration.</p>
<p>This is a convenience function for building a pool using <a href="https://docs.rs/crossbeam/0.8/crossbeam/fn.scope.html"><code>crossbeam::scope</code></a>
to spawn threads in a <a href="#method.spawn_handler"><code>spawn_handler</code></a>.
The threads in this pool will start by calling <code>wrapper</code>, which should
do initialization and continue by calling <code>ThreadBuilder::run()</code>.</p>
<h5 id="examples"><a href="#examples">Examples</a></h5>
<p>A scoped pool may be useful in combination with scoped thread-local variables.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code>
<span class="macro">scoped_tls::scoped_thread_local!</span>(<span class="kw">static </span>POOL_DATA: Vec&lt;i32&gt;);
<span class="kw">fn </span>main() -&gt; <span class="prelude-ty">Result</span>&lt;(), rayon::ThreadPoolBuildError&gt; {
<span class="kw">let </span>pool_data = <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];
<span class="comment">// We haven&#39;t assigned any TLS data yet.
</span><span class="macro">assert!</span>(!POOL_DATA.is_set());
rayon::ThreadPoolBuilder::new()
.build_scoped(
<span class="comment">// Borrow `pool_data` in TLS for each thread.
</span>|thread| POOL_DATA.set(<span class="kw-2">&amp;</span>pool_data, || thread.run()),
<span class="comment">// Do some work that needs the TLS data.
</span>|pool| pool.install(|| <span class="macro">assert!</span>(POOL_DATA.is_set())),
)<span class="question-mark">?</span>;
<span class="comment">// Once we&#39;ve returned, `pool_data` is no longer borrowed.
</span>drop(pool_data);
<span class="prelude-val">Ok</span>(())
}</code></pre></div>
</div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-ThreadPoolBuilder%3CS%3E-1" class="impl has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#352">source</a><a href="#impl-ThreadPoolBuilder%3CS%3E-1" class="anchor"></a><h3 class="code-header">impl&lt;S&gt; <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;</h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.spawn_handler" class="method has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#440-442">source</a><h4 class="code-header">pub fn <a href="#method.spawn_handler" class="fnname">spawn_handler</a>&lt;F&gt;(self, spawn: F) -&gt; <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;CustomSpawn&lt;F&gt;&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;F: FnMut(<a class="struct" href="struct.ThreadBuilder.html" title="struct rayon::ThreadBuilder">ThreadBuilder</a>) -&gt; Result&lt;(), Error&gt;,</span></h4></section></summary><div class="docblock"><p>Sets a custom function for spawning threads.</p>
<p>Note that the threads will not exit until after the pool is dropped. It
is up to the caller to wait for thread termination if that is important
for any invariants. For instance, threads created in <a href="https://docs.rs/crossbeam/0.8/crossbeam/fn.scope.html"><code>crossbeam::scope</code></a>
will be joined before that scope returns, and this will block indefinitely
if the pool is leaked. Furthermore, the global thread pool doesn’t terminate
until the entire process exits!</p>
<h5 id="examples-1"><a href="#examples-1">Examples</a></h5>
<p>A minimal spawn handler just needs to call <code>run()</code> from an independent thread.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>main() -&gt; <span class="prelude-ty">Result</span>&lt;(), rayon::ThreadPoolBuildError&gt; {
<span class="kw">let </span>pool = rayon::ThreadPoolBuilder::new()
.spawn_handler(|thread| {
std::thread::spawn(|| thread.run());
<span class="prelude-val">Ok</span>(())
})
.build()<span class="question-mark">?</span>;
pool.install(|| <span class="macro">println!</span>(<span class="string">&quot;Hello from my custom thread!&quot;</span>));
<span class="prelude-val">Ok</span>(())
}</code></pre></div>
<p>The default spawn handler sets the name and stack size if given, and propagates
any errors from the thread builder.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>main() -&gt; <span class="prelude-ty">Result</span>&lt;(), rayon::ThreadPoolBuildError&gt; {
<span class="kw">let </span>pool = rayon::ThreadPoolBuilder::new()
.spawn_handler(|thread| {
<span class="kw">let </span><span class="kw-2">mut </span>b = std::thread::Builder::new();
<span class="kw">if let </span><span class="prelude-val">Some</span>(name) = thread.name() {
b = b.name(name.to_owned());
}
<span class="kw">if let </span><span class="prelude-val">Some</span>(stack_size) = thread.stack_size() {
b = b.stack_size(stack_size);
}
b.spawn(|| thread.run())<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(())
})
.build()<span class="question-mark">?</span>;
pool.install(|| <span class="macro">println!</span>(<span class="string">&quot;Hello from my fully custom thread!&quot;</span>));
<span class="prelude-val">Ok</span>(())
}</code></pre></div>
<p>This can also be used for a pool of scoped threads like <a href="https://docs.rs/crossbeam/0.8/crossbeam/fn.scope.html"><code>crossbeam::scope</code></a>,
or <a href="https://doc.rust-lang.org/std/thread/fn.scope.html"><code>std::thread::scope</code></a> introduced in Rust 1.63, which is encapsulated in
<a href="#method.build_scoped"><code>build_scoped</code></a>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">fn </span>main() -&gt; <span class="prelude-ty">Result</span>&lt;(), rayon::ThreadPoolBuildError&gt; {
std::thread::scope(|scope| {
<span class="kw">let </span>pool = rayon::ThreadPoolBuilder::new()
.spawn_handler(|thread| {
<span class="kw">let </span><span class="kw-2">mut </span>builder = std::thread::Builder::new();
<span class="kw">if let </span><span class="prelude-val">Some</span>(name) = thread.name() {
builder = builder.name(name.to_string());
}
<span class="kw">if let </span><span class="prelude-val">Some</span>(size) = thread.stack_size() {
builder = builder.stack_size(size);
}
builder.spawn_scoped(scope, || {
<span class="comment">// Add any scoped initialization here, then run!
</span>thread.run()
})<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(())
})
.build()<span class="question-mark">?</span>;
pool.install(|| <span class="macro">println!</span>(<span class="string">&quot;Hello from my custom scoped thread!&quot;</span>));
<span class="prelude-val">Ok</span>(())
})
}</code></pre></div>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.thread_name" class="method has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#496-498">source</a><h4 class="code-header">pub fn <a href="#method.thread_name" class="fnname">thread_name</a>&lt;F&gt;(self, closure: F) -&gt; <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;F: 'static + FnMut(usize) -&gt; String,</span></h4></section></summary><div class="docblock"><p>Sets a closure which takes a thread index and returns
the thread’s name.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.num_threads" class="method has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#530">source</a><h4 class="code-header">pub fn <a href="#method.num_threads" class="fnname">num_threads</a>(self, num_threads: usize) -&gt; <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;</h4></section></summary><div class="docblock"><p>Sets the number of threads to be used in the rayon threadpool.</p>
<p>If you specify a non-zero number of threads using this
function, then the resulting thread-pools are guaranteed to
start at most this number of threads.</p>
<p>If <code>num_threads</code> is 0, or you do not call this function, then
the Rayon runtime will select the number of threads
automatically. At present, this is based on the
<code>RAYON_NUM_THREADS</code> environment variable (if set),
or the number of logical CPUs (otherwise).
In the future, however, the default behavior may
change to dynamically add or remove threads as needed.</p>
<p><strong>Future compatibility warning:</strong> Given the default behavior
may change in the future, if you wish to rely on a fixed
number of threads, you should use this function to specify
that number. To reproduce the current default behavior, you
may wish to use the <a href="https://crates.io/crates/num_cpus"><code>num_cpus</code>
crate</a> to query the number
of CPUs dynamically.</p>
<p><strong>Old environment variable:</strong> <code>RAYON_NUM_THREADS</code> is a one-to-one
replacement of the now deprecated <code>RAYON_RS_NUM_CPUS</code> environment
variable. If both variables are specified, <code>RAYON_NUM_THREADS</code> will
be preferred.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.panic_handler" class="method has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#554-556">source</a><h4 class="code-header">pub fn <a href="#method.panic_handler" class="fnname">panic_handler</a>&lt;H&gt;(self, panic_handler: H) -&gt; <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;H: 'static + Fn(Box&lt;dyn Any + Send + 'static, Global&gt;) + Send + Sync,</span></h4></section></summary><div class="docblock"><p>Normally, whenever Rayon catches a panic, it tries to
propagate it to someplace sensible, to try and reflect the
semantics of sequential execution. But in some cases,
particularly with the <code>spawn()</code> APIs, there is no
obvious place where we should propagate the panic to.
In that case, this panic handler is invoked.</p>
<p>If no panic handler is set, the default is to abort the
process, under the principle that panics should not go
unobserved.</p>
<p>If the panic handler itself panics, this will abort the
process. To prevent this, wrap the body of your panic handler
in a call to <code>std::panic::catch_unwind()</code>.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.stack_size" class="method has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#568">source</a><h4 class="code-header">pub fn <a href="#method.stack_size" class="fnname">stack_size</a>(self, stack_size: usize) -&gt; <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;</h4></section></summary><div class="docblock"><p>Sets the stack size of the worker threads</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.breadth_first" class="method has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#602">source</a><h4 class="code-header">pub fn <a href="#method.breadth_first" class="fnname">breadth_first</a>(self) -&gt; <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;</h4></section><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated: use <code>scope_fifo</code> and <code>spawn_fifo</code> for similar effect</span></div></span></summary><div class="docblock"><p><strong>(DEPRECATED)</strong> Suggest to worker threads that they execute
spawned jobs in a “breadth-first” fashion.</p>
<p>Typically, when a worker thread is idle or blocked, it will
attempt to execute the job from the <em>top</em> of its local deque of
work (i.e., the job most recently spawned). If this flag is set
to true, however, workers will prefer to execute in a
<em>breadth-first</em> fashion – that is, they will search for jobs at
the <em>bottom</em> of their local deque. (At present, workers <em>always</em>
steal from the bottom of other workers’ deques, regardless of
the setting of this flag.)</p>
<p>If you think of the tasks as a tree, where a parent task
spawns its children in the tree, then this flag loosely
corresponds to doing a breadth-first traversal of the tree,
whereas the default would be to do a depth-first traversal.</p>
<p><strong>Note that this is an “execution hint”.</strong> Rayon’s task
execution is highly dynamic and the precise order in which
independent tasks are executed is not intended to be
guaranteed.</p>
<p>This <code>breadth_first()</code> method is now deprecated per <a href="https://github.com/rayon-rs/rfcs/blob/master/accepted/rfc0001-scope-scheduling.md">RFC #1</a>,
and in the future its effect may be removed. Consider using
<a href="fn.scope_fifo.html"><code>scope_fifo()</code></a> for a similar effect.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.start_handler" class="method has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#622-624">source</a><h4 class="code-header">pub fn <a href="#method.start_handler" class="fnname">start_handler</a>&lt;H&gt;(self, start_handler: H) -&gt; <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;H: 'static + Fn(usize) + Send + Sync,</span></h4></section></summary><div class="docblock"><p>Sets a callback to be invoked on thread start.</p>
<p>The closure is passed the index of the thread on which it is invoked.
Note that this same closure may be invoked multiple times in parallel.
If this closure panics, the panic will be passed to the panic handler.
If that handler returns, then startup will continue normally.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.exit_handler" class="method has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#641-643">source</a><h4 class="code-header">pub fn <a href="#method.exit_handler" class="fnname">exit_handler</a>&lt;H&gt;(self, exit_handler: H) -&gt; <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;H: 'static + Fn(usize) + Send + Sync,</span></h4></section></summary><div class="docblock"><p>Sets a callback to be invoked on thread exit.</p>
<p>The closure is passed the index of the thread on which it is invoked.
Note that this same closure may be invoked multiple times in parallel.
If this closure panics, the panic will be passed to the panic handler.
If that handler returns, then the thread will exit normally.</p>
</div></details></div></details></div><h2 id="trait-implementations" class="small-section-header">Trait Implementations<a href="#trait-implementations" class="anchor"></a></h2><div id="trait-implementations-list"><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-Debug-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#770">source</a><a href="#impl-Debug-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;S&gt; Debug for <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;</h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.fmt" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#771">source</a><a href="#method.fmt" class="anchor"></a><h4 class="code-header">fn <a class="fnname">fmt</a>(&amp;self, f: &amp;mut Formatter&lt;'_&gt;) -&gt; Result&lt;(), Error&gt;</h4></section></summary><div class='docblock'>Formats the value using the given formatter. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle" open><summary><section id="impl-Default-for-ThreadPoolBuilder%3CDefaultSpawn%3E" class="impl has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#227">source</a><a href="#impl-Default-for-ThreadPoolBuilder%3CDefaultSpawn%3E" class="anchor"></a><h3 class="code-header">impl Default for <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;DefaultSpawn&gt;</h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.default" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/rayon_core/lib.rs.html#228">source</a><a href="#method.default" class="anchor"></a><h4 class="code-header">fn <a class="fnname">default</a>() -&gt; <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;DefaultSpawn&gt;</h4></section></summary><div class='docblock'>Returns the “default value” for a type. <a>Read more</a></div></details></div></details></div><h2 id="synthetic-implementations" class="small-section-header">Auto Trait Implementations<a href="#synthetic-implementations" class="anchor"></a></h2><div id="synthetic-implementations-list"><section id="impl-RefUnwindSafe-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a href="#impl-RefUnwindSafe-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;S&nbsp;=&nbsp;DefaultSpawn&gt; !RefUnwindSafe for <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;</h3></section><section id="impl-Send-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a href="#impl-Send-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;S&nbsp;=&nbsp;DefaultSpawn&gt; !Send for <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;</h3></section><section id="impl-Sync-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a href="#impl-Sync-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;S&nbsp;=&nbsp;DefaultSpawn&gt; !Sync for <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;</h3></section><section id="impl-Unpin-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a href="#impl-Unpin-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;S&gt; Unpin for <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;S: Unpin,</span></h3></section><section id="impl-UnwindSafe-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a href="#impl-UnwindSafe-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;S&nbsp;=&nbsp;DefaultSpawn&gt; !UnwindSafe for <a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a>&lt;S&gt;</h3></section></div><h2 id="blanket-implementations" class="small-section-header">Blanket Implementations<a href="#blanket-implementations" class="anchor"></a></h2><div id="blanket-implementations-list"><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Any-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a href="#impl-Any-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;T&gt; Any for T<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: 'static + ?Sized,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.type_id" class="method trait-impl has-srclink"><a href="#method.type_id" class="anchor"></a><h4 class="code-header">fn <a class="fnname">type_id</a>(&amp;self) -&gt; TypeId</h4></section></summary><div class='docblock'>Gets the <code>TypeId</code> of <code>self</code>. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Borrow%3CT%3E-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a href="#impl-Borrow%3CT%3E-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;T&gt; Borrow&lt;T&gt; for T<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: ?Sized,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.borrow" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/91522" title="Tracking issue for const_borrow">unstable</a></span><a href="#method.borrow" class="anchor"></a><h4 class="code-header">fn <a class="fnname">borrow</a>(&amp;self) -&gt; &amp;T</h4></section></summary><div class='docblock'>Immutably borrows from an owned value. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-BorrowMut%3CT%3E-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a href="#impl-BorrowMut%3CT%3E-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;T&gt; BorrowMut&lt;T&gt; for T<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: ?Sized,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.borrow_mut" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/91522" title="Tracking issue for const_borrow">unstable</a></span><a href="#method.borrow_mut" class="anchor"></a><h4 class="code-header">fn <a class="fnname">borrow_mut</a>(&amp;mut self) -&gt; &amp;mut T</h4></section></summary><div class='docblock'>Mutably borrows from an owned value. <a>Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-From%3CT%3E-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a href="#impl-From%3CT%3E-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;T&gt; From&lt;T&gt; for T</h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.from" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span><a href="#method.from" class="anchor"></a><h4 class="code-header">fn <a class="fnname">from</a>(t: T) -&gt; T</h4></section></summary><div class="docblock"><p>Returns the argument unchanged.</p>
</div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Into%3CU%3E-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a href="#impl-Into%3CU%3E-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;T, U&gt; Into&lt;U&gt; for T<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;U: From&lt;T&gt;,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.into" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span><a href="#method.into" class="anchor"></a><h4 class="code-header">fn <a class="fnname">into</a>(self) -&gt; U</h4></section></summary><div class="docblock"><p>Calls <code>U::from(self)</code>.</p>
<p>That is, this conversion is whatever the implementation of
<code>[From]&lt;T&gt; for U</code> chooses to do.</p>
</div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-Pointable-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a class="srclink rightside" href="../src/crossbeam_epoch/atomic.rs.html#194">source</a><a href="#impl-Pointable-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="../crossbeam_epoch/atomic/trait.Pointable.html" title="trait crossbeam_epoch::atomic::Pointable">Pointable</a> for T</h3></section></summary><div class="impl-items"><details class="rustdoc-toggle" open><summary><section id="associatedconstant.ALIGN" class="associatedconstant trait-impl has-srclink"><a class="srclink rightside" href="../src/crossbeam_epoch/atomic.rs.html#195">source</a><a href="#associatedconstant.ALIGN" class="anchor"></a><h4 class="code-header">const <a href="../crossbeam_epoch/atomic/trait.Pointable.html#associatedconstant.ALIGN" class="constant">ALIGN</a>: usize = mem::align_of::&lt;T&gt;()</h4></section></summary><div class='docblock'>The alignment of pointer.</div></details><details class="rustdoc-toggle" open><summary><section id="associatedtype.Init" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Init" class="anchor"></a><h4 class="code-header">type <a href="../crossbeam_epoch/atomic/trait.Pointable.html#associatedtype.Init" class="associatedtype">Init</a> = T</h4></section></summary><div class='docblock'>The type for initializers.</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.init" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/crossbeam_epoch/atomic.rs.html#199">source</a><a href="#method.init" class="anchor"></a><h4 class="code-header">unsafe fn <a href="../crossbeam_epoch/atomic/trait.Pointable.html#tymethod.init" class="fnname">init</a>(init: &lt;T as <a class="trait" href="../crossbeam_epoch/atomic/trait.Pointable.html" title="trait crossbeam_epoch::atomic::Pointable">Pointable</a>&gt;::<a class="associatedtype" href="../crossbeam_epoch/atomic/trait.Pointable.html#associatedtype.Init" title="type crossbeam_epoch::atomic::Pointable::Init">Init</a>) -&gt; usize</h4></section></summary><div class='docblock'>Initializes a with the given initializer. <a href="../crossbeam_epoch/atomic/trait.Pointable.html#tymethod.init">Read more</a></div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.deref" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/crossbeam_epoch/atomic.rs.html#203">source</a><a href="#method.deref" class="anchor"></a><h4 class="code-header">unsafe fn <a href="../crossbeam_epoch/atomic/trait.Pointable.html#tymethod.deref" class="fnname">deref</a>&lt;'a&gt;(ptr: usize) -&gt; &amp;'a T</h4></section></summary><div class='docblock'>Dereferences the given pointer. <a href="../crossbeam_epoch/atomic/trait.Pointable.html#tymethod.deref">Read more</a></div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.deref_mut" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/crossbeam_epoch/atomic.rs.html#207">source</a><a href="#method.deref_mut" class="anchor"></a><h4 class="code-header">unsafe fn <a href="../crossbeam_epoch/atomic/trait.Pointable.html#tymethod.deref_mut" class="fnname">deref_mut</a>&lt;'a&gt;(ptr: usize) -&gt; &amp;'a mut T</h4></section></summary><div class='docblock'>Mutably dereferences the given pointer. <a href="../crossbeam_epoch/atomic/trait.Pointable.html#tymethod.deref_mut">Read more</a></div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.drop" class="method trait-impl has-srclink"><a class="srclink rightside" href="../src/crossbeam_epoch/atomic.rs.html#211">source</a><a href="#method.drop" class="anchor"></a><h4 class="code-header">unsafe fn <a href="../crossbeam_epoch/atomic/trait.Pointable.html#tymethod.drop" class="fnname">drop</a>(ptr: usize)</h4></section></summary><div class='docblock'>Drops the object pointed to by the given pointer. <a href="../crossbeam_epoch/atomic/trait.Pointable.html#tymethod.drop">Read more</a></div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-TryFrom%3CU%3E-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a href="#impl-TryFrom%3CU%3E-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;T, U&gt; TryFrom&lt;U&gt; for T<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;U: Into&lt;T&gt;,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle" open><summary><section id="associatedtype.Error-1" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Error-1" class="anchor"></a><h4 class="code-header">type <a class="associatedtype">Error</a> = Infallible</h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_from" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span><a href="#method.try_from" class="anchor"></a><h4 class="code-header">fn <a class="fnname">try_from</a>(value: U) -&gt; Result&lt;T, &lt;T as TryFrom&lt;U&gt;&gt;::Error&gt;</h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details><details class="rustdoc-toggle implementors-toggle"><summary><section id="impl-TryInto%3CU%3E-for-ThreadPoolBuilder%3CS%3E" class="impl has-srclink"><a href="#impl-TryInto%3CU%3E-for-ThreadPoolBuilder%3CS%3E" class="anchor"></a><h3 class="code-header">impl&lt;T, U&gt; TryInto&lt;U&gt; for T<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;U: TryFrom&lt;T&gt;,</span></h3></section></summary><div class="impl-items"><details class="rustdoc-toggle" open><summary><section id="associatedtype.Error" class="associatedtype trait-impl has-srclink"><a href="#associatedtype.Error" class="anchor"></a><h4 class="code-header">type <a class="associatedtype">Error</a> = &lt;U as TryFrom&lt;T&gt;&gt;::Error</h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.try_into" class="method trait-impl has-srclink"><span class="since rightside" title="const unstable">const: <a href="https://github.com/rust-lang/rust/issues/88674" title="Tracking issue for const_convert">unstable</a></span><a href="#method.try_into" class="anchor"></a><h4 class="code-header">fn <a class="fnname">try_into</a>(self) -&gt; Result&lt;U, &lt;U as TryFrom&lt;T&gt;&gt;::Error&gt;</h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details></div></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>