Crates.io | barriers |
lib.rs | barriers |
version | 0.2.0 |
source | src |
created_at | 2021-05-01 20:37:20.967762 |
updated_at | 2021-05-01 20:46:36.154378 |
description | A barrier spin lock implementation |
homepage | |
repository | |
max_upload_size | |
id | 392013 |
size | 4,933 |
A barrier spin lock implementation in rust!
Just add the below to your Cargo.toml
dependencies.
barriers = "<version number>"
A barrier must first be initialized before using. A new barrier can be created by using the init
method and specifying the count. This barrier is a simple counting barrier that spins on a sense
variable.
let barr = barrier::Barrier::init(4); // 4 is the number of threads
Since barriers are generally shared between threads, it is a good idea to use an Arc
let barr = Arc::new(barrier::Barrier::init(4));
and then clone it before moving it to a new thread
for _ in 0..4 {
let barr_clone = Arc::clone(barr);
thread::spawn(move || {
barr_clone.arrive();
});
}
To pick a synchronization point, simply call the arrive
method.
barr_clone.arrive();