commit | 6eee6a40b40af1568f4fa8b2dbcd34cd53a2a464 | [log] [tgz] |
---|---|---|
author | Frank Piva <[email protected]> | Fri May 31 19:19:19 2024 +0000 |
committer | Frank Piva <[email protected]> | Fri May 31 19:19:19 2024 +0000 |
tree | 8076e3d75a836de2bd2d336ecba4c97dff415aa8 | |
parent | 14027f77b92e84fc6207638628ff84d87137d9d6 [diff] | |
parent | 67fd967596dfbd1619b8171f0d2112ef4a4316eb [diff] |
Merge remote-tracking branch 'origin/upstream'
A light-weight lock guarded by an atomic boolean.
Most efficient when contention is low, acquiring the lock is a single atomic swap, and releasing it just 1 more atomic swap.
use std::sync::Arc; use try_lock::TryLock; // a thing we want to share struct Widget { name: String, } // lock it up! let widget1 = Arc::new(TryLock::new(Widget { name: "Spanner".into(), })); let widget2 = widget1.clone(); // mutate the widget let mut locked = widget1.try_lock().expect("example isn't locked yet"); locked.name.push_str(" Bundle"); // hands off, buddy let not_locked = widget2.try_lock(); assert!(not_locked.is_none(), "widget1 has the lock"); // ok, you can have it drop(locked); let locked2 = widget2.try_lock().expect("widget1 lock is released"); assert_eq!(locked2.name, "Spanner Bundle");