Skip to content

Commit 150713c

Browse files
committed
Rewrite 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.
1 parent 045ca8b commit 150713c

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

src/libstd/thread/mod.rs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@
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. For example, the thread
118+
//! name is used in panic messages. By default, spawned threads are unnamed. To specify a name for
119+
//! a thread, build the thread with [`Builder`] and pass the desired thread name to
120+
//! [`Builder::name`]. To retrieve the thread name from within the thread, use [`Thread::name`].
121+
//!
122+
//! ## Stack size
123+
//!
124+
//! The default stack size for spawned threads is 2 MiB, though this particular stack size is
125+
//! subject to change in the future. There are two ways to manually specify the stack size for
126+
//! spawned threads:
127+
//!
128+
//! * Build the thread with [`Builder`] and pass the desired stack size to [`Builder::stack_size`].
129+
//! * Set the `RUST_MIN_STACK` environment variable to an integer representing the desired stack
130+
//! size (in bytes).
131+
//!
132+
//! Note that the stack size of the main thread is *not* determined by Rust.
133+
//!
115134
//! [channels]: ../../std/sync/mpsc/index.html
116135
//! [`Arc`]: ../../std/sync/struct.Arc.html
117136
//! [`spawn`]: ../../std/thread/fn.spawn.html
@@ -123,11 +142,14 @@
123142
//! [`Err`]: ../../std/result/enum.Result.html#variant.Err
124143
//! [`panic!`]: ../../std/macro.panic.html
125144
//! [`Builder`]: ../../std/thread/struct.Builder.html
145+
//! [`Builder::stack_size`]: ../../std/thread/struct.Builder.html#method.stack_size
146+
//! [`Builder::name`]: ../../std/thread/struct.Builder.html#method.name
126147
//! [`thread::current`]: ../../std/thread/fn.current.html
127148
//! [`thread::Result`]: ../../std/thread/type.Result.html
128149
//! [`Thread`]: ../../std/thread/struct.Thread.html
129150
//! [`park`]: ../../std/thread/fn.park.html
130151
//! [`unpark`]: ../../std/thread/struct.Thread.html#method.unpark
152+
//! [`Thread::name`]: ../../std/thread/struct.Thread.html#method.name
131153
//! [`thread::park_timeout`]: ../../std/thread/fn.park_timeout.html
132154
//! [`Cell`]: ../cell/struct.Cell.html
133155
//! [`RefCell`]: ../cell/struct.RefCell.html
@@ -187,16 +209,8 @@ pub use self::local::{LocalKey, LocalKeyState, AccessError};
187209
///
188210
/// The two configurations available are:
189211
///
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.
212+
/// - [`name`]: specifies an [associated name for the thread][naming-threads]
213+
/// - [`stack_size`]: specifies the [desired stack size for the thread][stack-size]
200214
///
201215
/// The [`spawn`] method will take ownership of the builder and create an
202216
/// [`io::Result`] to the thread handle with the given configuration.
@@ -228,6 +242,8 @@ pub use self::local::{LocalKey, LocalKeyState, AccessError};
228242
/// [`spawn`]: ../../std/thread/struct.Builder.html#method.spawn
229243
/// [`io::Result`]: ../../std/io/type.Result.html
230244
/// [`unwrap`]: ../../std/result/enum.Result.html#method.unwrap
245+
/// [naming-threads]: ./index.html#naming-threads
246+
/// [stack-size]: ./index.html#stack-size
231247
#[stable(feature = "rust1", since = "1.0.0")]
232248
#[derive(Debug)]
233249
pub struct Builder {
@@ -267,6 +283,9 @@ impl Builder {
267283
/// Names the thread-to-be. Currently the name is used for identification
268284
/// only in panic messages.
269285
///
286+
/// For more information about named threads, see
287+
/// [this module-level documentation][naming-threads].
288+
///
270289
/// # Examples
271290
///
272291
/// ```
@@ -281,6 +300,8 @@ impl Builder {
281300
///
282301
/// handler.join().unwrap();
283302
/// ```
303+
///
304+
/// [naming-threads]: ./index.html#naming-threads
284305
#[stable(feature = "rust1", since = "1.0.0")]
285306
pub fn name(mut self, name: String) -> Builder {
286307
self.name = Some(name);
@@ -292,13 +313,18 @@ impl Builder {
292313
/// The actual stack size may be greater than this value if
293314
/// the platform specifies minimal stack size.
294315
///
316+
/// For more information about the stack size for threads, see
317+
/// [this module-level documentation][stack-size].
318+
///
295319
/// # Examples
296320
///
297321
/// ```
298322
/// use std::thread;
299323
///
300324
/// let builder = thread::Builder::new().stack_size(32 * 1024);
301325
/// ```
326+
///
327+
/// [stack-size]: ./index.html#stack-size
302328
#[stable(feature = "rust1", since = "1.0.0")]
303329
pub fn stack_size(mut self, size: usize) -> Builder {
304330
self.stack_size = Some(size);
@@ -987,6 +1013,9 @@ impl Thread {
9871013

9881014
/// Gets the thread's name.
9891015
///
1016+
/// For more information about named threads, see
1017+
/// [this module-level documentation][naming-threads].
1018+
///
9901019
/// # Examples
9911020
///
9921021
/// Threads by default have no name specified:
@@ -1017,6 +1046,8 @@ impl Thread {
10171046
///
10181047
/// handler.join().unwrap();
10191048
/// ```
1049+
///
1050+
/// [naming-threads]: ./index.html#naming-threads
10201051
#[stable(feature = "rust1", since = "1.0.0")]
10211052
pub fn name(&self) -> Option<&str> {
10221053
self.cname().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) } )

0 commit comments

Comments
 (0)