• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H03-May-2022-272138

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D10-Jul-202074 65

.gitignoreH A D15-Mar-201831 53

Cargo.tomlH A D10-Jul-2020938 2724

Cargo.toml.orig-cargoH A D10-Jul-2020448 1513

LICENSEH A D15-Mar-20181.1 KiB2217

README.mdH A D15-Mar-2018926 4529

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