-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #41207 - sstewartgallus:spinpause, r=alexcrichton
Spin loop pause function redux GitHub's interface is screwy. This is the same PR as #40537
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/doc/unstable-book/src/library-features/hint-core-should-pause.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters