core::sync::atomic::spin_loop_hint - Rust
Description: API documentation for the Rust `spin_loop_hint` fn in crate `core`.
Keywords: rust, rustlang, rust-lang, spin_loop_hint

# 1.24.0[−][src]Function core::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.

Spin locks can be very efficient for short lock durations because they do not involve context switches or interaction with the operating system. For long lock durations they become wasteful however because they use CPU cycles for the entire lock duration, and using a `std::sync::Mutex` is likely the better approach. If actively spinning for a long time is required, e.g. because code polls a non-blocking API, calling `std::thread::yield_now` or `std::thread::sleep` may be the best option.

Note: Spin locks are based on the underlying assumption that another thread will release the lock 'soon'. In order for this to work, that other thread must run on a different CPU or core (at least potentially). Spin locks do not work efficiently on single CPU / core platforms.

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

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)