Module priomutex::reservable
[−]
[src]
A reservable mutex (spinlock-only)
A reservable mutex will be freed as usual when the guard goes out of scope, but it can be explicity
freed by calling the release_to
method on the guard. In this case, it's possible to specify a
thread to reserve the mutex for. In this case, try_lock
will succeed only if called from the
specified thread.
Reservable mutex comes with no blocking support. As such, the API is exactly like that of
std::sync::Mutex
except that there is no lock
. Instead, waiting threads should spin until
try_lock
succeeds. The lack of blocking support means that this mutex is implemented with no
syscalls or platform-specific functionality.
Reservable mutex comes with no support for poisoning: if a thread panics while holding the lock, the mutex will be freed normally.
use priomutex::reservable::Mutex; use std::sync::Arc; use std::thread; const N: usize = 10; let data = Arc::new(Mutex::new(0)); let mut threads = vec![]; for _ in 0..N { let data = data.clone(); threads.push(thread::spawn(move || { let mut data = loop { if let Some(x) = data.try_lock() { break x } else { thread::yield_now(); } }; *data += 1; // non-atomic increment })); } for t in threads { t.join(); } // data should have been incremented 10 times assert_eq!(*data.try_lock().unwrap(), N);
Structs
Mutex |
A mutex with no blocking support, but the ability to reserve the lock for another thread. |
MutexGuard |
An RAII guard. Frees the mutex when dropped. |