A spinlock.
Dangerous to use in userspace, do not use unless you know what you're doing. Can lead to priority inversion and other nasty stuff. Prefer to use Mutex instead.
Example
use std::sync::Spinlock;
use std::thread::spawn;
let lock = Spinlock::new(); // prefer to use Mutex instead
let sync_print = |&lock, t: &[u8]| {
lock.lock();
defer lock.unlock();
println!("{}", t);
};
let t1 = spawn(|=sync_print| { sync_print("hello world 1") });
let t2 = spawn(|=sync_print| { sync_print("hello world 2") });
t1.join().unwrap();
t2.join().unwrap();
Run this example
Fields
Methods
impl Spinlock { ... }
-
fn new() -> Spinlock
Creates a new mutex.
-
fn lock(self: &mut Spinlock)
Acquires a spinlock, spinning until it is acquired.
-
Attempts to acquire a spinlock, but does not spin.
Returns
true
if the lock was successfully acquired,false
otherwise. -
Attempts to acquire a spinlock, spining for up to
times
times.Returns
true
if the lock was successfully acquired,false
otherwise. -
fn unlock(self: &mut Spinlock)
Releases a spinlock.