Skip to content

Commit 644fc40

Browse files
committed
Auto merge of #41814 - gamazeps:thread-struct-doc, r=steveklabnik
[Doc] improve `thread::Thread` and `thread::Builder` documentations Part of #29378 - Adds information about the stack_size when using `Builder`. This might be considered too low level, but I assume that if someone wants to create their own builder instead of using `thread::spawn` they may be interested in that info. - Updates the `thread::Thread` structure doc, mostly by explaining how to get one, the previous example was removed because it was not related to `thread::Thread`, but rather to `thread::Builder::name`. Not much is present there, mostly because this API is not often used (the only method that seems useful is `unpark`, which is documented in #41809).
2 parents f3fc547 + 323a774 commit 644fc40

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

src/libstd/thread/mod.rs

+20-23
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ impl Builder {
244244
/// Generates the base configuration for spawning a thread, from which
245245
/// configuration methods can be chained.
246246
///
247+
/// If the [`stack_size`] field is not specified, the stack size
248+
/// will be the `RUST_MIN_STACK` environment variable, if it is
249+
/// not specified either, a sensible default size will be set (2MB as
250+
/// of the writting of this doc).
251+
///
247252
/// # Examples
248253
///
249254
/// ```
@@ -259,6 +264,8 @@ impl Builder {
259264
///
260265
/// handler.join().unwrap();
261266
/// ```
267+
///
268+
/// [`stack_size`]: ../../std/thread/struct.Builder.html#method.stack_size
262269
#[stable(feature = "rust1", since = "1.0.0")]
263270
pub fn new() -> Builder {
264271
Builder {
@@ -728,31 +735,21 @@ struct Inner {
728735
#[stable(feature = "rust1", since = "1.0.0")]
729736
/// A handle to a thread.
730737
///
731-
/// You can use it to identify a thread (by name, for example). Most of the
732-
/// time, there is no need to directly create a `Thread` struct using the
733-
/// constructor, instead you should use a function like `spawn` to create
734-
/// new threads, see the docs of [`Builder`] and [`spawn`] for more.
738+
/// Threads are represented via the `Thread` type, which you can get in one of
739+
/// two ways:
735740
///
736-
/// # Examples
741+
/// * By spawning a new thread, e.g. using the [`thread::spawn`][`spawn`]
742+
/// function, and calling [`thread`][`JoinHandle::thread`] on the
743+
/// [`JoinHandle`].
744+
/// * By requesting the current thread, using the [`thread::current`] function.
745+
///
746+
/// The [`thread::current`] function is available even for threads not spawned
747+
/// by the APIs of this module.
748+
///
749+
/// There is usualy no need to create a `Thread` struct yourself, one
750+
/// should instead use a function like `spawn` to create new threads, see the
751+
/// docs of [`Builder`] and [`spawn`] for more details.
737752
///
738-
/// ```no_run
739-
/// # // Note that this example isn't executed by default because it causes
740-
/// # // deadlocks on Windows unfortunately (see #25824)
741-
/// use std::thread::Builder;
742-
///
743-
/// for i in 0..5 {
744-
/// let thread_name = format!("thread_{}", i);
745-
/// Builder::new()
746-
/// .name(thread_name) // Now you can identify which thread panicked
747-
/// // thanks to the handle's name
748-
/// .spawn(move || {
749-
/// if i == 3 {
750-
/// panic!("I'm scared!!!");
751-
/// }
752-
/// })
753-
/// .unwrap();
754-
/// }
755-
/// ```
756753
/// [`Builder`]: ../../std/thread/struct.Builder.html
757754
/// [`spawn`]: ../../std/thread/fn.spawn.html
758755

0 commit comments

Comments
 (0)