pub struct SyncStopper { /* private fields */ }Expand description
A cancellation primitive with Release/Acquire memory ordering.
Unlike Stopper which uses Relaxed ordering,
SyncStopper guarantees that all writes before cancel() are visible
to any clone that subsequently observes should_stop() == true.
§Example
use almost_enough::{SyncStopper, Stop};
let stop = SyncStopper::new();
let stop2 = stop.clone();
// In producer thread:
// ... write shared data ...
stop.cancel(); // Release barrier
// In consumer thread:
if stop2.should_stop() { // Acquire barrier
// Safe to read shared data written before cancel()
}§Performance
On x86/x64, Release/Acquire has negligible overhead (strong memory model).
On ARM and other weakly-ordered architectures, there’s a small cost for
the memory barriers. Use Stopper if you don’t
need the synchronization guarantees.
Implementations§
Source§impl SyncStopper
impl SyncStopper
Sourcepub fn cancel(&self)
pub fn cancel(&self)
Cancel with Release ordering.
All memory writes before this call are guaranteed to be visible
to any clone that subsequently observes should_stop() == true.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Check if cancelled with Acquire ordering.
If this returns true, all memory writes that happened before
the corresponding cancel() call are guaranteed to be visible.
Trait Implementations§
Source§impl Clone for SyncStopper
impl Clone for SyncStopper
Source§fn clone(&self) -> SyncStopper
fn clone(&self) -> SyncStopper
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more