Skip to content

Commit 5edcf9b

Browse files
authored
Rollup merge of #43848 - frewsxcv:frewsxcv-stack-size, r=QuietMisdreavus
Rewrite/reorganize docs for stack size/thread names for spawned threads. * Moves docs about stack size and thread naming from `Builder` to the `std::thread` module * Adds more links to the new module-level documentation * Mentions the 2 MiB stack size default, but indicate it's subject to change Fixes #43805.
2 parents c200953 + 6594601 commit 5edcf9b

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

src/libstd/thread/mod.rs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,29 @@
112112
//! will want to make use of some form of **interior mutability** through the
113113
//! [`Cell`] or [`RefCell`] types.
114114
//!
115+
//! ## Naming threads
116+
//!
117+
//! Threads are able to have associated names for identification purposes. By default, spawned
118+
//! threads are unnamed. To specify a name for a thread, build the thread with [`Builder`] and pass
119+
//! the desired thread name to [`Builder::name`]. To retrieve the thread name from within the
120+
//! thread, use [`Thread::name`]. A couple examples of where the name of a thread gets used:
121+
//!
122+
//! * If a panic occurs in a named thread, the thread name will be printed in the panic message.
123+
//! * The thread name is provided to the OS where applicable (e.g. `pthread_setname_np` in
124+
//! unix-like platforms).
125+
//!
126+
//! ## Stack size
127+
//!
128+
//! The default stack size for spawned threads is 2 MiB, though this particular stack size is
129+
//! subject to change in the future. There are two ways to manually specify the stack size for
130+
//! spawned threads:
131+
//!
132+
//! * Build the thread with [`Builder`] and pass the desired stack size to [`Builder::stack_size`].
133+
//! * Set the `RUST_MIN_STACK` environment variable to an integer representing the desired stack
134+
//! size (in bytes). Note that setting [`Builder::stack_size`] will override this.
135+
//!
136+
//! Note that the stack size of the main thread is *not* determined by Rust.
137+
//!
115138
//! [channels]: ../../std/sync/mpsc/index.html
116139
//! [`Arc`]: ../../std/sync/struct.Arc.html
117140
//! [`spawn`]: ../../std/thread/fn.spawn.html
@@ -123,11 +146,14 @@
123146
//! [`Err`]: ../../std/result/enum.Result.html#variant.Err
124147
//! [`panic!`]: ../../std/macro.panic.html
125148
//! [`Builder`]: ../../std/thread/struct.Builder.html
149+
//! [`Builder::stack_size`]: ../../std/thread/struct.Builder.html#method.stack_size
150+
//! [`Builder::name`]: ../../std/thread/struct.Builder.html#method.name
126151
//! [`thread::current`]: ../../std/thread/fn.current.html
127152
//! [`thread::Result`]: ../../std/thread/type.Result.html
128153
//! [`Thread`]: ../../std/thread/struct.Thread.html
129154
//! [`park`]: ../../std/thread/fn.park.html
130155
//! [`unpark`]: ../../std/thread/struct.Thread.html#method.unpark
156+
//! [`Thread::name`]: ../../std/thread/struct.Thread.html#method.name
131157
//! [`thread::park_timeout`]: ../../std/thread/fn.park_timeout.html
132158
//! [`Cell`]: ../cell/struct.Cell.html
133159
//! [`RefCell`]: ../cell/struct.RefCell.html
@@ -187,16 +213,8 @@ pub use self::local::{LocalKey, LocalKeyState, AccessError};
187213
///
188214
/// The two configurations available are:
189215
///
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 overridden 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.
216+
/// - [`name`]: specifies an [associated name for the thread][naming-threads]
217+
/// - [`stack_size`]: specifies the [desired stack size for the thread][stack-size]
200218
///
201219
/// The [`spawn`] method will take ownership of the builder and create an
202220
/// [`io::Result`] to the thread handle with the given configuration.
@@ -228,6 +246,8 @@ pub use self::local::{LocalKey, LocalKeyState, AccessError};
228246
/// [`spawn`]: ../../std/thread/struct.Builder.html#method.spawn
229247
/// [`io::Result`]: ../../std/io/type.Result.html
230248
/// [`unwrap`]: ../../std/result/enum.Result.html#method.unwrap
249+
/// [naming-threads]: ./index.html#naming-threads
250+
/// [stack-size]: ./index.html#stack-size
231251
#[stable(feature = "rust1", since = "1.0.0")]
232252
#[derive(Debug)]
233253
pub struct Builder {
@@ -267,6 +287,9 @@ impl Builder {
267287
/// Names the thread-to-be. Currently the name is used for identification
268288
/// only in panic messages.
269289
///
290+
/// For more information about named threads, see
291+
/// [this module-level documentation][naming-threads].
292+
///
270293
/// # Examples
271294
///
272295
/// ```
@@ -281,6 +304,8 @@ impl Builder {
281304
///
282305
/// handler.join().unwrap();
283306
/// ```
307+
///
308+
/// [naming-threads]: ./index.html#naming-threads
284309
#[stable(feature = "rust1", since = "1.0.0")]
285310
pub fn name(mut self, name: String) -> Builder {
286311
self.name = Some(name);
@@ -292,13 +317,18 @@ impl Builder {
292317
/// The actual stack size may be greater than this value if
293318
/// the platform specifies minimal stack size.
294319
///
320+
/// For more information about the stack size for threads, see
321+
/// [this module-level documentation][stack-size].
322+
///
295323
/// # Examples
296324
///
297325
/// ```
298326
/// use std::thread;
299327
///
300328
/// let builder = thread::Builder::new().stack_size(32 * 1024);
301329
/// ```
330+
///
331+
/// [stack-size]: ./index.html#stack-size
302332
#[stable(feature = "rust1", since = "1.0.0")]
303333
pub fn stack_size(mut self, size: usize) -> Builder {
304334
self.stack_size = Some(size);
@@ -987,6 +1017,9 @@ impl Thread {
9871017

9881018
/// Gets the thread's name.
9891019
///
1020+
/// For more information about named threads, see
1021+
/// [this module-level documentation][naming-threads].
1022+
///
9901023
/// # Examples
9911024
///
9921025
/// Threads by default have no name specified:
@@ -1017,6 +1050,8 @@ impl Thread {
10171050
///
10181051
/// handler.join().unwrap();
10191052
/// ```
1053+
///
1054+
/// [naming-threads]: ./index.html#naming-threads
10201055
#[stable(feature = "rust1", since = "1.0.0")]
10211056
pub fn name(&self) -> Option<&str> {
10221057
self.cname().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) } )

0 commit comments

Comments
 (0)