112
112
//! will want to make use of some form of **interior mutability** through the
113
113
//! [`Cell`] or [`RefCell`] types.
114
114
//!
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
+ //!
115
138
//! [channels]: ../../std/sync/mpsc/index.html
116
139
//! [`Arc`]: ../../std/sync/struct.Arc.html
117
140
//! [`spawn`]: ../../std/thread/fn.spawn.html
123
146
//! [`Err`]: ../../std/result/enum.Result.html#variant.Err
124
147
//! [`panic!`]: ../../std/macro.panic.html
125
148
//! [`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
126
151
//! [`thread::current`]: ../../std/thread/fn.current.html
127
152
//! [`thread::Result`]: ../../std/thread/type.Result.html
128
153
//! [`Thread`]: ../../std/thread/struct.Thread.html
129
154
//! [`park`]: ../../std/thread/fn.park.html
130
155
//! [`unpark`]: ../../std/thread/struct.Thread.html#method.unpark
156
+ //! [`Thread::name`]: ../../std/thread/struct.Thread.html#method.name
131
157
//! [`thread::park_timeout`]: ../../std/thread/fn.park_timeout.html
132
158
//! [`Cell`]: ../cell/struct.Cell.html
133
159
//! [`RefCell`]: ../cell/struct.RefCell.html
@@ -187,16 +213,8 @@ pub use self::local::{LocalKey, LocalKeyState, AccessError};
187
213
///
188
214
/// The two configurations available are:
189
215
///
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]
200
218
///
201
219
/// The [`spawn`] method will take ownership of the builder and create an
202
220
/// [`io::Result`] to the thread handle with the given configuration.
@@ -228,6 +246,8 @@ pub use self::local::{LocalKey, LocalKeyState, AccessError};
228
246
/// [`spawn`]: ../../std/thread/struct.Builder.html#method.spawn
229
247
/// [`io::Result`]: ../../std/io/type.Result.html
230
248
/// [`unwrap`]: ../../std/result/enum.Result.html#method.unwrap
249
+ /// [naming-threads]: ./index.html#naming-threads
250
+ /// [stack-size]: ./index.html#stack-size
231
251
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
232
252
#[ derive( Debug ) ]
233
253
pub struct Builder {
@@ -267,6 +287,9 @@ impl Builder {
267
287
/// Names the thread-to-be. Currently the name is used for identification
268
288
/// only in panic messages.
269
289
///
290
+ /// For more information about named threads, see
291
+ /// [this module-level documentation][naming-threads].
292
+ ///
270
293
/// # Examples
271
294
///
272
295
/// ```
@@ -281,6 +304,8 @@ impl Builder {
281
304
///
282
305
/// handler.join().unwrap();
283
306
/// ```
307
+ ///
308
+ /// [naming-threads]: ./index.html#naming-threads
284
309
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
285
310
pub fn name ( mut self , name : String ) -> Builder {
286
311
self . name = Some ( name) ;
@@ -292,13 +317,18 @@ impl Builder {
292
317
/// The actual stack size may be greater than this value if
293
318
/// the platform specifies minimal stack size.
294
319
///
320
+ /// For more information about the stack size for threads, see
321
+ /// [this module-level documentation][stack-size].
322
+ ///
295
323
/// # Examples
296
324
///
297
325
/// ```
298
326
/// use std::thread;
299
327
///
300
328
/// let builder = thread::Builder::new().stack_size(32 * 1024);
301
329
/// ```
330
+ ///
331
+ /// [stack-size]: ./index.html#stack-size
302
332
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
303
333
pub fn stack_size ( mut self , size : usize ) -> Builder {
304
334
self . stack_size = Some ( size) ;
@@ -987,6 +1017,9 @@ impl Thread {
987
1017
988
1018
/// Gets the thread's name.
989
1019
///
1020
+ /// For more information about named threads, see
1021
+ /// [this module-level documentation][naming-threads].
1022
+ ///
990
1023
/// # Examples
991
1024
///
992
1025
/// Threads by default have no name specified:
@@ -1017,6 +1050,8 @@ impl Thread {
1017
1050
///
1018
1051
/// handler.join().unwrap();
1019
1052
/// ```
1053
+ ///
1054
+ /// [naming-threads]: ./index.html#naming-threads
1020
1055
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1021
1056
pub fn name ( & self ) -> Option < & str > {
1022
1057
self . cname ( ) . map ( |s| unsafe { str:: from_utf8_unchecked ( s. to_bytes ( ) ) } )
0 commit comments