Skip to content

Commit 375b264

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

File tree

1 file changed

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

1 file changed

+46
-0
lines changed

rayon-core/src/thread_pool/mod.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,52 @@ impl ThreadPool {
8080
/// thread-local data from the current thread will not be
8181
/// accessible.
8282
///
83+
/// # Warning: execution order
84+
///
85+
/// If the current thread is part of a different thread pool, it will try to
86+
/// keep busy while the `op` completes in its target pool, similar to
87+
/// calling [`ThreadPool::yield_now()`] in a loop. Therefore, it may
88+
/// potentially schedule other tasks to run on the current thread in the
89+
/// meantime. For example
90+
///
91+
/// ```rust
92+
/// # use rayon_core as rayon;
93+
/// fn do_it(pool: &rayon::ThreadPool) -> () {
94+
/// print!("one ");
95+
/// pool.install(||{});
96+
/// print!("two ");
97+
/// }
98+
/// fn do_it_x_times(x: i32, pool: &rayon::ThreadPool) -> () {
99+
/// if x > 0 {
100+
/// do_it(pool);
101+
/// rayon::join(
102+
/// || do_it_x_times(x-1, pool),
103+
/// || do_it_x_times(x-2, pool),
104+
/// );
105+
/// }
106+
/// }
107+
/// fn main() {
108+
/// rayon::ThreadPoolBuilder::new().num_threads(1).build_global().unwrap();
109+
/// let pool = rayon_core::ThreadPoolBuilder::default().build().unwrap();
110+
/// do_it_x_times(3, &pool);
111+
/// }
112+
/// ```
113+
///
114+
/// Since we configured just one thread in the global pool, one might
115+
/// expect `do_it()` to run sequentially, producing:
116+
///
117+
/// ```ascii
118+
/// one two one two one two
119+
/// ```
120+
///
121+
/// However each call to `install()` yields implicitly, allowing rayon to
122+
/// run several instances of `do_it()` concurrently on the single, global
123+
/// thread. The following output would be equally valid:
124+
///
125+
/// ```ascii
126+
/// one one two one two two
127+
/// ```
128+
///
83129
/// # Panics
84130
///
85131
/// If `op` should panic, that panic will be propagated.

0 commit comments

Comments
 (0)