]> git.lizzy.rs Git - rust.git/blob - src/docs/missing_spin_loop.txt
Auto merge of #9425 - kraktus:patch-1, r=xFrednet
[rust.git] / src / docs / missing_spin_loop.txt
1 ### What it does
2 Check for empty spin loops
3
4 ### Why is this bad?
5 The loop body should have something like `thread::park()` or at least
6 `std::hint::spin_loop()` to avoid needlessly burning cycles and conserve
7 energy. Perhaps even better use an actual lock, if possible.
8
9 ### Known problems
10 This lint doesn't currently trigger on `while let` or
11 `loop { match .. { .. } }` loops, which would be considered idiomatic in
12 combination with e.g. `AtomicBool::compare_exchange_weak`.
13
14 ### Example
15
16 ```
17 use core::sync::atomic::{AtomicBool, Ordering};
18 let b = AtomicBool::new(true);
19 // give a ref to `b` to another thread,wait for it to become false
20 while b.load(Ordering::Acquire) {};
21 ```
22 Use instead:
23 ```
24 while b.load(Ordering::Acquire) {
25     std::hint::spin_loop()
26 }
27 ```