W3cubDocs

/Rust

Function std::sync::atomic::spin_loop_hint

pub fn spin_loop_hint()

Signals the processor that it is inside a busy-wait spin-loop ("spin lock").

Upon receiving spin-loop signal the processor can optimize its behavior by, for example, saving power or switching hyper-threads.

This function is different from std::thread::yield_now which directly yields to the system's scheduler, whereas spin_loop_hint does not interact with the operating system.

A common use case for spin_loop_hint is implementing bounded optimistic spinning in a CAS loop in synchronization primitives. To avoid problems like priority inversion, it is strongly recommended that the spin loop is terminated after a finite amount of iterations and an appropriate blocking syscall is made.

Note: On platforms that do not support receiving spin-loop hints this function does not do anything at all.

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/sync/atomic/fn.spin_loop_hint.html