Skip to content

Commit bccd9ee

Browse files
committed
Added documentation about thread hooks to panics and errors in thread spawning functions.
1 parent f889772 commit bccd9ee

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

library/std/src/thread/builder.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,18 @@ impl Builder {
160160
/// [`io::Result`] to capture any failure to create the thread at
161161
/// the OS level.
162162
///
163+
/// Like [`spawn`], this method will still call the main thread functions
164+
/// added by [`add_spawn_hook`] (unless [`Builder::no_hooks`] was called),
165+
/// but won't execute the returned functions if thread creation fails at the
166+
/// OS level.
167+
///
163168
/// # Panics
164169
///
165170
/// Panics if a thread name was set and it contained null bytes.
166171
///
172+
/// Unlike the [`spawn`] free function, if it panics for this reason, the
173+
/// parent thread functions added by [`add_spawn_hook`] will _not_ be called.
174+
///
167175
/// # Examples
168176
///
169177
/// ```
@@ -178,8 +186,10 @@ impl Builder {
178186
/// handler.join().unwrap();
179187
/// ```
180188
///
189+
/// [`io::Result`]: crate::io::Result
181190
/// [`thread::spawn`]: super::spawn
182191
/// [`spawn`]: super::spawn
192+
/// [`add_spawn_hook`]: crate::thread::add_spawn_hook
183193
#[stable(feature = "rust1", since = "1.0.0")]
184194
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
185195
pub fn spawn<F, T>(self, f: F) -> io::Result<JoinHandle<T>>
@@ -209,10 +219,18 @@ impl Builder {
209219
/// [`io::Result`] to capture any failure to create the thread at
210220
/// the OS level.
211221
///
222+
/// Like [`spawn`], this method will still call the main thread functions
223+
/// added by [`add_spawn_hook`] (unless [`Builder::no_hooks`] was called),
224+
/// but won't execute the returned functions if thread creation fails at the
225+
/// OS level.
226+
///
212227
/// # Panics
213228
///
214229
/// Panics if a thread name was set and it contained null bytes.
215230
///
231+
/// Unlike the [`spawn`] free function, if it panics for this reason, the
232+
/// parent thread functions added by [`add_spawn_hook`] will _not_ be called.
233+
///
216234
/// # Safety
217235
///
218236
/// The caller has to ensure that the spawned thread does not outlive any
@@ -249,6 +267,7 @@ impl Builder {
249267
///
250268
/// [`thread::spawn`]: super::spawn
251269
/// [`spawn`]: super::spawn
270+
/// [`add_spawn_hook`]: crate::thread::add_spawn_hook
252271
#[stable(feature = "thread_spawn_unchecked", since = "1.82.0")]
253272
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
254273
pub unsafe fn spawn_unchecked<F, T>(self, f: F) -> io::Result<JoinHandle<T>>

library/std/src/thread/functions.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ use crate::{io, panicking};
4545
///
4646
/// # Panics
4747
///
48-
/// Panics if the OS fails to create a thread; use [`Builder::spawn`]
49-
/// to recover from such errors.
48+
/// Panics if the OS fails to create a thread; use [`Builder::spawn`] to recover
49+
/// from such errors.
50+
///
51+
/// Additionally, if hooks were added via [`add_spawn_hook`], they will still be
52+
/// called in the parent thread, but the returned functions will not be executed.
5053
///
5154
/// # Examples
5255
///
@@ -120,6 +123,7 @@ use crate::{io, panicking};
120123
/// [`channels`]: crate::sync::mpsc
121124
/// [`join`]: JoinHandle::join
122125
/// [`Err`]: crate::result::Result::Err
126+
/// [`add_spawn_hook`]: crate::thread::add_spawn_hook
123127
#[stable(feature = "rust1", since = "1.0.0")]
124128
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
125129
pub fn spawn<F, T>(f: F) -> JoinHandle<T>

library/std/src/thread/scoped.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,13 @@ impl<'scope, 'env> Scope<'scope, 'env> {
196196
/// Panics if the OS fails to create a thread; use [`Builder::spawn_scoped`]
197197
/// to recover from such errors.
198198
///
199+
/// like the free [`spawn`] function, if hooks were added via [`add_spawn_hook`],
200+
/// they will still be called in the parent thread, but the returned functions will
201+
/// not be executed.
202+
///
199203
/// [`join`]: ScopedJoinHandle::join
204+
/// [`add_spawn_hook`]: crate::thread::add_spawn_hook
205+
/// [`spawn`]: crate::thread::spawn
200206
#[stable(feature = "scoped_threads", since = "1.63.0")]
201207
pub fn spawn<F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
202208
where
@@ -213,10 +219,18 @@ impl Builder {
213219
/// Unlike [`Scope::spawn`], this method yields an [`io::Result`] to
214220
/// capture any failure to create the thread at the OS level.
215221
///
222+
/// Like [`Scope::spawn`], this method will still call the main thread functions
223+
/// added by [`add_spawn_hook`] (unless [`Builder::no_hooks`] was called),
224+
/// but won't execute the returned functions if thread creation fails at the
225+
/// OS level.
226+
///
216227
/// # Panics
217228
///
218229
/// Panics if a thread name was set and it contained null bytes.
219230
///
231+
/// Unlike with [`Scope::spawn`], if it panics for this reason, the hooks
232+
/// added by [`add_spawn_hook`] will _not_ be called.
233+
///
220234
/// # Example
221235
///
222236
/// ```
@@ -252,6 +266,9 @@ impl Builder {
252266
/// a.push(4);
253267
/// assert_eq!(x, a.len());
254268
/// ```
269+
///
270+
/// [`io::Result`]: crate::io::Result
271+
/// [`add_spawn_hook`]: crate::thread::add_spawn_hook
255272
#[stable(feature = "scoped_threads", since = "1.63.0")]
256273
pub fn spawn_scoped<'scope, 'env, F, T>(
257274
self,

0 commit comments

Comments
 (0)