Skip to content

Commit 8e24971

Browse files
committed
Document implicit yield in install() per rayon-rs#1105
1 parent d1b18e6 commit 8e24971

File tree

1 file changed

+35
-0
lines changed
  • rayon-core/src/thread_pool

1 file changed

+35
-0
lines changed

rayon-core/src/thread_pool/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,41 @@ impl ThreadPool {
8080
/// thread-local data from the current thread will not be
8181
/// accessible.
8282
///
83+
/// # Warning: execution order
84+
///
85+
/// `install()` implicitly calls [`ThreadPool::yield_now()`],
86+
/// potentially scheduling another task to run on the current thread. For
87+
/// example:
88+
///
89+
/// ```rust
90+
/// # use rayon_core as rayon;
91+
/// use rayon::iter::{IntoParallelIterator, ParallelIterator};
92+
/// fn main() {
93+
/// rayon::ThreadPoolBuilder::new().num_threads(1).build_global().unwrap();
94+
/// let pool = rayon::ThreadPoolBuilder::default().build().unwrap();
95+
/// (0..3).into_par_iter().for_each(|_| {
96+
/// print!("one ");
97+
/// pool.install(||{});
98+
/// print!("two ");
99+
/// });
100+
/// }
101+
/// ```
102+
///
103+
/// Since we configured just one thread in the global pool, one might
104+
/// expect this to run sequentially, producing:
105+
///
106+
/// ```ascii
107+
/// one two one two one two
108+
/// ```
109+
///
110+
/// However each call to `install()` yields implicitly, allowing rayon to
111+
/// run several instances of the `for_each()` loop concurrently on the
112+
/// single, global thread. The following output would be equally valid:
113+
///
114+
/// ```ascii
115+
/// one one two one two two
116+
/// ```
117+
///
83118
/// # Panics
84119
///
85120
/// If `op` should panic, that panic will be propagated.

0 commit comments

Comments
 (0)