|
17 | 17 | //! provide some built-in support for low-level synchronization. |
18 | 18 | //! |
19 | 19 | //! Communication between threads can be done through |
20 | | -//! [channels](../../std/sync/mpsc/index.html), Rust's message-passing |
21 | | -//! types, along with [other forms of thread |
| 20 | +//! [channels], Rust's message-passing types, along with [other forms of thread |
22 | 21 | //! synchronization](../../std/sync/index.html) and shared-memory data |
23 | 22 | //! structures. In particular, types that are guaranteed to be |
24 | 23 | //! threadsafe are easily shared between threads using the |
25 | | -//! atomically-reference-counted container, |
26 | | -//! [`Arc`](../../std/sync/struct.Arc.html). |
| 24 | +//! atomically-reference-counted container, [`Arc`]. |
27 | 25 | //! |
28 | 26 | //! Fatal logic errors in Rust cause *thread panic*, during which |
29 | 27 | //! a thread will unwind the stack, running destructors and freeing |
|
40 | 38 | //! |
41 | 39 | //! ## Spawning a thread |
42 | 40 | //! |
43 | | -//! A new thread can be spawned using the `thread::spawn` function: |
| 41 | +//! A new thread can be spawned using the [`thread::spawn`][`spawn`] function: |
44 | 42 | //! |
45 | 43 | //! ```rust |
46 | 44 | //! use std::thread; |
|
55 | 53 | //! it), unless this parent is the main thread. |
56 | 54 | //! |
57 | 55 | //! The parent thread can also wait on the completion of the child |
58 | | -//! thread; a call to `spawn` produces a `JoinHandle`, which provides |
| 56 | +//! thread; a call to [`spawn`] produces a [`JoinHandle`], which provides |
59 | 57 | //! a `join` method for waiting: |
60 | 58 | //! |
61 | 59 | //! ```rust |
|
68 | 66 | //! let res = child.join(); |
69 | 67 | //! ``` |
70 | 68 | //! |
71 | | -//! The `join` method returns a `Result` containing `Ok` of the final |
72 | | -//! value produced by the child thread, or `Err` of the value given to |
73 | | -//! a call to `panic!` if the child panicked. |
| 69 | +//! The [`join`] method returns a [`Result`] containing [`Ok`] of the final |
| 70 | +//! value produced by the child thread, or [`Err`] of the value given to |
| 71 | +//! a call to [`panic!`] if the child panicked. |
74 | 72 | //! |
75 | 73 | //! ## Configuring threads |
76 | 74 | //! |
77 | | -//! A new thread can be configured before it is spawned via the `Builder` type, |
| 75 | +//! A new thread can be configured before it is spawned via the [`Builder`] type, |
78 | 76 | //! which currently allows you to set the name and stack size for the child thread: |
79 | 77 | //! |
80 | 78 | //! ```rust |
|
88 | 86 | //! |
89 | 87 | //! ## The `Thread` type |
90 | 88 | //! |
91 | | -//! Threads are represented via the `Thread` type, which you can get in one of |
| 89 | +//! Threads are represented via the [`Thread`] type, which you can get in one of |
92 | 90 | //! two ways: |
93 | 91 | //! |
94 | | -//! * By spawning a new thread, e.g. using the `thread::spawn` function, and |
95 | | -//! calling `thread()` on the `JoinHandle`. |
96 | | -//! * By requesting the current thread, using the `thread::current` function. |
| 92 | +//! * By spawning a new thread, e.g. using the [`thread::spawn`][`spawn`] |
| 93 | +//! function, and calling [`thread()`] on the [`JoinHandle`]. |
| 94 | +//! * By requesting the current thread, using the [`thread::current()`] function. |
97 | 95 | //! |
98 | | -//! The `thread::current()` function is available even for threads not spawned |
| 96 | +//! The [`thread::current()`] function is available even for threads not spawned |
99 | 97 | //! by the APIs of this module. |
100 | 98 | //! |
101 | 99 | //! ## Blocking support: park and unpark |
102 | 100 | //! |
103 | 101 | //! Every thread is equipped with some basic low-level blocking support, via the |
104 | | -//! `thread::park()` function and `thread::Thread::unpark()` method. `park()` |
105 | | -//! blocks the current thread, which can then be resumed from another thread by |
106 | | -//! calling the `unpark()` method on the blocked thread's handle. |
| 102 | +//! [`thread::park()`][`park()`] function and [`thread::Thread::unpark()`][`unpark()`] |
| 103 | +//! method. [`park()`] blocks the current thread, which can then be resumed from |
| 104 | +//! another thread by calling the [`unpark()`] method on the blocked thread's handle. |
107 | 105 | //! |
108 | | -//! Conceptually, each `Thread` handle has an associated token, which is |
| 106 | +//! Conceptually, each [`Thread`] handle has an associated token, which is |
109 | 107 | //! initially not present: |
110 | 108 | //! |
111 | | -//! * The `thread::park()` function blocks the current thread unless or until |
| 109 | +//! * The [`thread::park()`][`park()`] function blocks the current thread unless or until |
112 | 110 | //! the token is available for its thread handle, at which point it atomically |
113 | 111 | //! consumes the token. It may also return *spuriously*, without consuming the |
114 | | -//! token. `thread::park_timeout()` does the same, but allows specifying a |
| 112 | +//! token. [`thread::park_timeout()`] does the same, but allows specifying a |
115 | 113 | //! maximum time to block the thread for. |
116 | 114 | //! |
117 | | -//! * The `unpark()` method on a `Thread` atomically makes the token available |
| 115 | +//! * The [`unpark()`] method on a [`Thread`] atomically makes the token available |
118 | 116 | //! if it wasn't already. |
119 | 117 | //! |
120 | | -//! In other words, each `Thread` acts a bit like a semaphore with initial count |
| 118 | +//! In other words, each [`Thread`] acts a bit like a semaphore with initial count |
121 | 119 | //! 0, except that the semaphore is *saturating* (the count cannot go above 1), |
122 | 120 | //! and can return spuriously. |
123 | 121 | //! |
124 | 122 | //! The API is typically used by acquiring a handle to the current thread, |
125 | 123 | //! placing that handle in a shared data structure so that other threads can |
126 | 124 | //! find it, and then `park`ing. When some desired condition is met, another |
127 | | -//! thread calls `unpark` on the handle. |
| 125 | +//! thread calls [`unpark()`] on the handle. |
128 | 126 | //! |
129 | 127 | //! The motivation for this design is twofold: |
130 | 128 | //! |
|
149 | 147 | //! will want to make use of some form of **interior mutability** through the |
150 | 148 | //! [`Cell`] or [`RefCell`] types. |
151 | 149 | //! |
| 150 | +//! [channels]: ../../std/sync/mpsc/index.html |
| 151 | +//! [`Arc`]: ../../std/sync/struct.Arc.html |
| 152 | +//! [`spawn`]: ../../std/thread/fn.spawn.html |
| 153 | +//! [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html |
| 154 | +//! [`thread()`]: ../../std/thread/struct.JoinHandle.html#method.thread |
| 155 | +//! [`join`]: ../../std/thread/struct.JoinHandle.html#method.join |
| 156 | +//! [`Result`]: ../../std/result/enum.Result.html |
| 157 | +//! [`Ok`]: ../../std/result/enum.Result.html#variant.Ok |
| 158 | +//! [`Err`]: ../../std/result/enum.Result.html#variant.Err |
| 159 | +//! [`panic!`]: ../../std/macro.panic.html |
| 160 | +//! [`Builder`]: ../../std/thread/struct.Builder.html |
| 161 | +//! [`thread::current()`]: ../../std/thread/fn.spawn.html |
| 162 | +//! [`Thread`]: ../../std/thread/struct.Thread.html |
| 163 | +//! [`park()`]: ../../std/thread/fn.park.html |
| 164 | +//! [`unpark()`]: ../../std/thread/struct.Thread.html#method.unpark |
| 165 | +//! [`thread::park_timeout()`]: ../../std/thread/fn.park_timeout.html |
152 | 166 | //! [`Cell`]: ../cell/struct.Cell.html |
153 | 167 | //! [`RefCell`]: ../cell/struct.RefCell.html |
154 | 168 | //! [`thread_local!`]: ../macro.thread_local.html |
|
0 commit comments