Thread synchronization primitives
Modules
Multi-threaded queues (channels)
Protocols
Protocol for types implementing mutex-like semantics.
Structs
Values that can be operated on atomically.
A mutual exclusion lock (mutex)
Reader-writer lock
Condition variable
A spinlock.
A synchronization primitive that allows multiple threads to wait until the
event is signalled.
A one-shot channel (future).
Functions
-
fn fence(ordering: Ordering)
Memory barrier
-
fn compiler_fence(ordering: Ordering)
Compiler-only memory barrier
This prevents the compiler from reordering memory accesses according to the memory access ordering, but does not prevent hardware reordering.
-
A hint to the CPU that we are spinning
This does not result in a system call like yield_thread, but can reduce power usage or allow other hyperthreads to run.
Example
use std::sync::{Atomic, Ordering, spin_loop}; let flag = Atomic::new(false); while !flag.exchange(true, Ordering::Acquire) { spin_loop(); }
Run this example -
fn with_lock<L, T, F>(lock: &mut L, f: F) -> T
L: Lockable<L>F: Fn() -> TExecute a closure with the lock held
Example
use std::sync::{Mutex, with_lock}; let mutex = Mutex::new(); mutex.with_lock(|| { println!("Hello, World"); });
Run this example
Enums
Memory ordering