@@ -50,15 +50,28 @@ pub unsafe fn unreachable_unchecked() -> ! {
50
50
intrinsics:: unreachable ( )
51
51
}
52
52
53
- /// Save power or switch hyperthreads in a busy-wait spin-loop.
53
+ /// Signals the processor that it is entering a busy-wait spin-loop.
54
54
///
55
- /// This function is deliberately more primitive than
56
- /// [`std::thread::yield_now`](../../std/thread/fn.yield_now.html) and
57
- /// does not directly yield to the system's scheduler.
58
- /// In some cases it might be useful to use a combination of both functions.
59
- /// Careful benchmarking is advised.
55
+ /// Upon receiving spin-loop signal the processor can optimize its behavior by, for example, saving
56
+ /// power or switching hyper-threads.
60
57
///
61
- /// On some platforms this function may not do anything at all.
58
+ /// This function is different than [`std::thread::yield_now`] which directly yields to the
59
+ /// system's scheduler, whereas `spin_loop` only signals the processor that it is entering a
60
+ /// busy-wait spin-loop without yielding control to the system's scheduler.
61
+ ///
62
+ /// Using a busy-wait spin-loop with `spin_loop` is ideally used in situations where a
63
+ /// contended lock is held by another thread executed on a different CPU and where the waiting
64
+ /// times are relatively small. Because entering busy-wait spin-loop does not trigger the system's
65
+ /// scheduler, no overhead for switching threads occurs. However, if the thread holding the
66
+ /// contended lock is running on the same CPU, the spin-loop is likely to occupy an entire CPU slice
67
+ /// before switching to the thread that holds the lock. If the contending lock is held by a thread
68
+ /// on the same CPU or if the waiting times for acquiring the lock are longer, it is often better to
69
+ /// use [`std::thread::yield_now`].
70
+ ///
71
+ /// **Note**: On platforms that do not support receiving spin-loop hints this function does not
72
+ /// do anything at all.
73
+ ///
74
+ /// [`std::thread::yield_now`]: ../../../std/thread/fn.yield_now.html
62
75
#[ inline]
63
76
#[ unstable( feature = "renamed_spin_loop" , issue = "55002" ) ]
64
77
pub fn spin_loop ( ) {
0 commit comments