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. 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
+ //!
115
134
//! [channels]: ../../std/sync/mpsc/index.html
116
135
//! [`Arc`]: ../../std/sync/struct.Arc.html
117
136
//! [`spawn`]: ../../std/thread/fn.spawn.html
123
142
//! [`Err`]: ../../std/result/enum.Result.html#variant.Err
124
143
//! [`panic!`]: ../../std/macro.panic.html
125
144
//! [`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
126
147
//! [`thread::current`]: ../../std/thread/fn.current.html
127
148
//! [`thread::Result`]: ../../std/thread/type.Result.html
128
149
//! [`Thread`]: ../../std/thread/struct.Thread.html
129
150
//! [`park`]: ../../std/thread/fn.park.html
130
151
//! [`unpark`]: ../../std/thread/struct.Thread.html#method.unpark
152
+ //! [`Thread::name`]: ../../std/thread/struct.Thread.html#method.name
131
153
//! [`thread::park_timeout`]: ../../std/thread/fn.park_timeout.html
132
154
//! [`Cell`]: ../cell/struct.Cell.html
133
155
//! [`RefCell`]: ../cell/struct.RefCell.html
@@ -187,16 +209,8 @@ pub use self::local::{LocalKey, LocalKeyState, AccessError};
187
209
///
188
210
/// The two configurations available are:
189
211
///
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]
200
214
///
201
215
/// The [`spawn`] method will take ownership of the builder and create an
202
216
/// [`io::Result`] to the thread handle with the given configuration.
@@ -228,6 +242,8 @@ pub use self::local::{LocalKey, LocalKeyState, AccessError};
228
242
/// [`spawn`]: ../../std/thread/struct.Builder.html#method.spawn
229
243
/// [`io::Result`]: ../../std/io/type.Result.html
230
244
/// [`unwrap`]: ../../std/result/enum.Result.html#method.unwrap
245
+ /// [naming-threads]: ./index.html#naming-threads
246
+ /// [stack-size]: ./index.html#stack-size
231
247
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
232
248
#[ derive( Debug ) ]
233
249
pub struct Builder {
@@ -267,6 +283,9 @@ impl Builder {
267
283
/// Names the thread-to-be. Currently the name is used for identification
268
284
/// only in panic messages.
269
285
///
286
+ /// For more information about named threads, see
287
+ /// [this module-level documentation][naming-threads].
288
+ ///
270
289
/// # Examples
271
290
///
272
291
/// ```
@@ -281,6 +300,8 @@ impl Builder {
281
300
///
282
301
/// handler.join().unwrap();
283
302
/// ```
303
+ ///
304
+ /// [naming-threads]: ./index.html#naming-threads
284
305
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
285
306
pub fn name ( mut self , name : String ) -> Builder {
286
307
self . name = Some ( name) ;
@@ -292,13 +313,18 @@ impl Builder {
292
313
/// The actual stack size may be greater than this value if
293
314
/// the platform specifies minimal stack size.
294
315
///
316
+ /// For more information about the stack size for threads, see
317
+ /// [this module-level documentation][stack-size].
318
+ ///
295
319
/// # Examples
296
320
///
297
321
/// ```
298
322
/// use std::thread;
299
323
///
300
324
/// let builder = thread::Builder::new().stack_size(32 * 1024);
301
325
/// ```
326
+ ///
327
+ /// [stack-size]: ./index.html#stack-size
302
328
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
303
329
pub fn stack_size ( mut self , size : usize ) -> Builder {
304
330
self . stack_size = Some ( size) ;
@@ -987,6 +1013,9 @@ impl Thread {
987
1013
988
1014
/// Gets the thread's name.
989
1015
///
1016
+ /// For more information about named threads, see
1017
+ /// [this module-level documentation][naming-threads].
1018
+ ///
990
1019
/// # Examples
991
1020
///
992
1021
/// Threads by default have no name specified:
@@ -1017,6 +1046,8 @@ impl Thread {
1017
1046
///
1018
1047
/// handler.join().unwrap();
1019
1048
/// ```
1049
+ ///
1050
+ /// [naming-threads]: ./index.html#naming-threads
1020
1051
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1021
1052
pub fn name ( & self ) -> Option < & str > {
1022
1053
self . cname ( ) . map ( |s| unsafe { str:: from_utf8_unchecked ( s. to_bytes ( ) ) } )
0 commit comments