Crates.io | spinlocks |
lib.rs | spinlocks |
version | 0.0.1 |
source | src |
created_at | 2015-03-09 21:06:51.648852 |
updated_at | 2015-12-11 23:56:18.321051 |
description | Lock primitive based on spinning protecting shared data for concurrent access |
homepage | |
repository | https://github.com/jorisgio/spinlock.rs |
max_upload_size | |
id | 1545 |
size | 19,770 |
A spinlock implementation in rust
Run cargo build
The library implements a Reader/Writer lock. When locking a spin lock for shared Read access, you will get a reference to the protected data, and while locking for an exclusive Write access, you will get a mutable reference.
extern crate spinlock;
use spinlock::SpinLock;
fn main() {
let spin = SpinLock::new(0);
// Write access
{
let mut data = spin.write().unwrap();
*data += 1;
}
// Read access
{
let data = spin.read().unwrap();
println!("{}", *data);
}
}
Please note that the spinlock doesn't deal itself with reference counting. You
might want to use Arc<SpinLock<T>>
to share the lock between threads.
The implementation is derived from the spinlock implementation written by Matt Dillon for DragonFlyBSD