Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | 03-May-2022 | - | ||||
src/ | H | 03-May-2022 | - | 272 | 138 | |
.cargo-checksum.json | H A D | 03-May-2022 | 89 | 1 | 1 | |
.cargo_vcs_info.json | H A D | 10-Jul-2020 | 74 | 6 | 5 | |
.gitignore | H A D | 15-Mar-2018 | 31 | 5 | 3 | |
Cargo.toml | H A D | 10-Jul-2020 | 938 | 27 | 24 | |
Cargo.toml.orig-cargo | H A D | 10-Jul-2020 | 448 | 15 | 13 | |
LICENSE | H A D | 15-Mar-2018 | 1.1 KiB | 22 | 17 | |
README.md | H A D | 15-Mar-2018 | 926 | 45 | 29 |
README.md
1# TryLock 2 3- [Crates.io](https://crates.io/crates/try-lock) 4- [Docs](https://docs.rs/try-lock) 5 6A light-weight lock guarded by an atomic boolean. 7 8Most efficient when contention is low, acquiring the lock is a single atomic swap, and releasing it just 1 more atomic swap. 9 10## Example 11 12```rust 13use std::sync::Arc; 14use try_lock::TryLock; 15 16// a thing we want to share 17struct Widget { 18 name: String, 19} 20 21// lock it up! 22let widget1 = Arc::new(TryLock::new(Widget { 23 name: "Spanner".into(), 24})); 25 26let widget2 = widget1.clone(); 27 28 29// mutate the widget 30let mut locked = widget1.try_lock().expect("example isn't locked yet"); 31locked.name.push_str(" Bundle"); 32 33// hands off, buddy 34let not_locked = widget2.try_lock(); 35assert!(not_locked.is_none(), "widget1 has the lock"); 36 37// ok, you can have it 38drop(locked); 39 40let locked2 = widget2.try_lock().expect("widget1 lock is released"); 41 42assert_eq!(locked2.name, "Spanner Bundle"); 43``` 44 45