spin-sync is a module providing synchronization primitives using spinlock. (Wikipedia Spinlock)
The main features are as follows.
Mutex
, RwLock
, Once
, Barrier
. The interfaces are resembles those of std::sync
.std::sync
, including poisoning strategy and marker traits.std::sync
, the constructors of the public structs are const; i.e. it is possible to declare
static Mutex<T>
as long as T can be build statically.Add the following line in dependencies section in your Cargo.toml.
Cargo.toml
spin-sync = "0.2.1"
Build, test and run your project.
shell
cargo build
cargo test
cargo run
Declare static spin_sync::Mutex<u64>
variable and update from multi threads.
It is impossible in case of std::sync::Mutex
.
``` extern crate spin_sync;
use spin_sync::Mutex; use std::thread;
// Declare static mut Mutex
fn main() { let num_thread = 10; let mut handles = Vec::new();
// Create worker threads to inclement COUNT by 1.
for _ in 0..10 {
let handle = thread::spawn(move || {
let mut count = COUNT.lock().unwrap();
*count += 1;
});
handles.push(handle);
}
// Wait for all the workers.
for handle in handles {
handle.join().unwrap();
}
// Make sure the value is incremented by the worker count.
let count = COUNT.lock().unwrap();
assert_eq!(num_thread, *count);
} ```