Skip to content

Commit 18171a8

Browse files
Rollup merge of #41994 - gamazeps:thread-builder, r=GuillaumeGomez
[Doc] Implrove `thread::Builder`'s doc. Part of #29378 . - Explains *why* we would use the builder instead ofthe free function. - Changes the parent-child explanation for a spawned-caller in `thread::Builder::spawn` - Adds a link to `io::Result` in `thread::Builder` - Corrects the return type doc in `thread::Builder::spawn` r? @rust-lang/docs
2 parents efe285e + a51777e commit 18171a8

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

src/libstd/thread/mod.rs

+38-11
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,33 @@ pub use self::local::{LocalKey, LocalKeyState};
180180
// Builder
181181
////////////////////////////////////////////////////////////////////////////////
182182

183-
/// Thread configuration. Provides detailed control over the properties
184-
/// and behavior of new threads.
183+
/// Thread factory, which can be used in order to configure the properties of
184+
/// a new thread.
185+
///
186+
/// Methods can be chained on it in order to configure it.
187+
///
188+
/// The two configurations available are:
189+
///
190+
/// - [`name`]: allows to give a name to the thread which is currently
191+
/// only used in `panic` messages.
192+
/// - [`stack_size`]: specifies the desired stack size. Note that this can
193+
/// be overriden by the OS.
194+
///
195+
/// If the [`stack_size`] field is not specified, the stack size
196+
/// will be the `RUST_MIN_STACK` environment variable. If it is
197+
/// not specified either, a sensible default will be set.
198+
///
199+
/// If the [`name`] field is not specified, the thread will not be named.
200+
///
201+
/// The [`spawn`] method will take ownership of the builder and create an
202+
/// [`io::Result`] to the thread handle with the given configuration.
203+
///
204+
/// The [`thread::spawn`] free function uses a `Builder` with default
205+
/// configuration and [`unwrap`]s its return value.
206+
///
207+
/// You may want to use [`spawn`] instead of [`thread::spawn`], when you want
208+
/// to recover from a failure to launch a thread, indeed the free function will
209+
/// panick where the `Builder` method will return a [`io::Result`].
185210
///
186211
/// # Examples
187212
///
@@ -196,6 +221,13 @@ pub use self::local::{LocalKey, LocalKeyState};
196221
///
197222
/// handler.join().unwrap();
198223
/// ```
224+
///
225+
/// [`thread::spawn`]: ../../std/thread/fn.spawn.html
226+
/// [`stack_size`]: ../../std/thread/struct.Builder.html#method.stack_size
227+
/// [`name`]: ../../std/thread/struct.Builder.html#method.name
228+
/// [`spawn`]: ../../std/thread/struct.Builder.html#method.spawn
229+
/// [`io::Result`]: ../../std/io/type.Result.html
230+
/// [`unwrap`]: ../../std/result/enum.Result.html#method.unwrap
199231
#[stable(feature = "rust1", since = "1.0.0")]
200232
#[derive(Debug)]
201233
pub struct Builder {
@@ -209,11 +241,6 @@ impl Builder {
209241
/// Generates the base configuration for spawning a thread, from which
210242
/// configuration methods can be chained.
211243
///
212-
/// If the [`stack_size`] field is not specified, the stack size
213-
/// will be the `RUST_MIN_STACK` environment variable. If it is
214-
/// not specified either, a sensible default will be set (2MB as
215-
/// of the writting of this doc).
216-
///
217244
/// # Examples
218245
///
219246
/// ```
@@ -229,8 +256,6 @@ impl Builder {
229256
///
230257
/// handler.join().unwrap();
231258
/// ```
232-
///
233-
/// [`stack_size`]: ../../std/thread/struct.Builder.html#method.stack_size
234259
#[stable(feature = "rust1", since = "1.0.0")]
235260
pub fn new() -> Builder {
236261
Builder {
@@ -280,9 +305,10 @@ impl Builder {
280305
self
281306
}
282307

283-
/// Spawns a new thread, and returns a join handle for it.
308+
/// Spawns a new thread by taking ownership of the `Builder`, and returns an
309+
/// [`io::Result`] to its [`JoinHandle`].
284310
///
285-
/// The child thread may outlive the parent (unless the parent thread
311+
/// The spawned thread may outlive the caller (unless the caller thread
286312
/// is the main thread; the whole process is terminated when the main
287313
/// thread finishes). The join handle can be used to block on
288314
/// termination of the child thread, including recovering its panics.
@@ -297,6 +323,7 @@ impl Builder {
297323
///
298324
/// [`spawn`]: ../../std/thread/fn.spawn.html
299325
/// [`io::Result`]: ../../std/io/type.Result.html
326+
/// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html
300327
///
301328
/// # Examples
302329
///

0 commit comments

Comments
 (0)