Added spin loop pause function

This commit is contained in:
Steven Stewart-Gallus 2017-03-14 21:28:31 -07:00 committed by Steven Stewart-Gallus
parent 777ee20796
commit f4fe3cd0e9
3 changed files with 65 additions and 0 deletions

View file

@ -145,6 +145,7 @@
- [future_atomic_orderings](library-features/future-atomic-orderings.md)
- [get_type_id](library-features/get-type-id.md)
- [heap_api](library-features/heap-api.md)
- [hint_core_should_pause](library-features/hint-core-should-pause.md)
- [i128](library-features/i128.md)
- [inclusive_range](library-features/inclusive-range.md)
- [integer_atomics](library-features/integer-atomics.md)

View file

@ -0,0 +1,41 @@
# `hint_core_should_pause`
The tracking issue for this feature is: [#41196]
[#41196]: https://github.com/rust-lang/rust/issues/41196
------------------------
Many programs have spin loops like the following:
```rust,no_run
use std::sync::atomic::{AtomicBool,Ordering};
fn spin_loop(value: &AtomicBool) {
loop {
if value.load(Ordering::Acquire) {
break;
}
}
}
```
These programs can be improved in performance like so:
```rust,no_run
#![feature(hint_core_should_pause)]
use std::sync::atomic;
use std::sync::atomic::{AtomicBool,Ordering};
fn spin_loop(value: &AtomicBool) {
loop {
if value.load(Ordering::Acquire) {
break;
}
atomic::hint_core_should_pause();
}
}
```
Further improvements could combine `hint_core_should_pause` with
exponential backoff or `std::thread::yield_now`.