diff options
| author | Mark Simulacrum <mark.simulacrum@gmail.com> | 2017-05-16 08:18:29 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-05-16 08:18:29 -0600 |
| commit | efe285e0ff2007206fd23d7d2b6b21720a52e610 (patch) | |
| tree | d4dee8ba52149633cb2fbec26de219f154728ea8 /src/libstd/thread | |
| parent | 7a9cf929d5befc5022d60a6f610178d383e64a33 (diff) | |
| parent | a9cb094879fabd23774a0d9dcfed7f3eeb9a4aff (diff) | |
| download | rust-efe285e0ff2007206fd23d7d2b6b21720a52e610.tar.gz rust-efe285e0ff2007206fd23d7d2b6b21720a52e610.zip | |
Rollup merge of #41982 - gamazeps:thread-yield-now, r=GuillaumeGomez
[Doc] Explain why `thread::yield_now` could be used. Part of #29378. r? @steveklabnik
Diffstat (limited to 'src/libstd/thread')
| -rw-r--r-- | src/libstd/thread/mod.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 200368be275..8f1a88ed305 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -468,6 +468,23 @@ pub fn current() -> Thread { /// Cooperatively gives up a timeslice to the OS scheduler. /// +/// This is used when the programmer knows that the thread will have nothing +/// to do for some time, and thus avoid wasting computing time. +/// +/// For example when polling on a resource, it is common to check that it is +/// available, and if not to yield in order to avoid busy waiting. +/// +/// Thus the pattern of `yield`ing after a failed poll is rather common when +/// implementing low-level shared resources or synchronization primitives. +/// +/// However programmers will usualy prefer to use, [`channel`]s, [`Condvar`]s, +/// [`Mutex`]es or [`join`] for their synchronisation routines, as they avoid +/// thinking about thread schedulling. +/// +/// Note that [`channel`]s for example are implemented using this primitive. +/// Indeed when you call `send` or `recv`, which are blocking, they will yield +/// if the channel is not available. +/// /// # Examples /// /// ``` @@ -475,6 +492,12 @@ pub fn current() -> Thread { /// /// thread::yield_now(); /// ``` +/// +/// [`channel`]: ../../std/sync/mpsc/index.html +/// [`spawn`]: ../../std/thread/fn.spawn.html +/// [`join`]: ../../std/thread/struct.JoinHandle.html#method.join +/// [`Mutex`]: ../../std/sync/struct.Mutex.html +/// [`Condvar`]: ../../std/sync/struct.Condvar.html #[stable(feature = "rust1", since = "1.0.0")] pub fn yield_now() { imp::Thread::yield_now() |
