@@ -124,28 +124,31 @@ use crate::fmt;
124124
125125use crate :: hint:: spin_loop;
126126
127- /// Signals the processor that it is entering a busy-wait spin-loop.
127+ /// Signals the processor that it is inside a busy-wait spin-loop ("spin lock") .
128128///
129129/// Upon receiving spin-loop signal the processor can optimize its behavior by, for example, saving
130130/// power or switching hyper-threads.
131131///
132- /// This function is different than [`std::thread::yield_now`] which directly yields to the
133- /// system's scheduler, whereas `spin_loop_hint` only signals the processor that it is entering a
134- /// busy-wait spin-loop without yielding control to the system's scheduler.
132+ /// This function is different from [`std::thread::yield_now`] which directly yields to the
133+ /// system's scheduler, whereas `spin_loop_hint` does not interact with the operating system.
135134///
136- /// Using a busy-wait spin-loop with `spin_loop_hint` is ideally used in situations where a
137- /// contended lock is held by another thread executed on a different CPU and where the waiting
138- /// times are relatively small. Because entering busy-wait spin-loop does not trigger the system's
139- /// scheduler, no overhead for switching threads occurs. However, if the thread holding the
140- /// contended lock is running on the same CPU, the spin-loop is likely to occupy an entire CPU slice
141- /// before switching to the thread that holds the lock. If the contending lock is held by a thread
142- /// on the same CPU or if the waiting times for acquiring the lock are longer, it is often better to
143- /// use [`std::thread::yield_now`].
135+ /// Spin locks can be very efficient for short lock durations because they do not involve context
136+ /// switches or interaction with the operating system. For long lock durations they become wasteful
137+ /// however because they use CPU cycles for the entire lock duration, and using a
138+ /// [`std::sync::Mutex`] is likely the better approach. If actively spinning for a long time is
139+ /// required, e.g. because code polls a non-blocking API, calling [`std::thread::yield_now`]
140+ /// or [`std::thread::sleep`] may be the best option.
141+ ///
142+ /// **Note**: Spin locks are based on the underlying assumption that another thread will release
143+ /// the lock 'soon'. In order for this to work, that other thread must run on a different CPU or
144+ /// core (at least potentially). Spin locks do not work efficiently on single CPU / core platforms.
144145///
145146/// **Note**: On platforms that do not support receiving spin-loop hints this function does not
146147/// do anything at all.
147148///
148149/// [`std::thread::yield_now`]: ../../../std/thread/fn.yield_now.html
150+ /// [`std::thread::sleep`]: ../../../std/thread/fn.sleep.html
151+ /// [`std::sync::Mutex`]: ../../../std/sync/struct.Mutex.html
149152#[ inline]
150153#[ stable( feature = "spin_loop_hint" , since = "1.24.0" ) ]
151154pub fn spin_loop_hint ( ) {
0 commit comments