|
1 |
| -# async-lock |
| 1 | +# async-io |
2 | 2 |
|
3 |
| -[]( |
4 |
| -https://github.com/stjepang/async-lock/actions) |
| 3 | +[]( |
| 4 | +https://github.com/stjepang/async-io/actions) |
5 | 5 | [](
|
6 |
| -https://github.com/stjepang/async-lock) |
7 |
| -[]( |
8 |
| -https://crates.io/crates/async-lock) |
9 |
| -[]( |
10 |
| -https://docs.rs/async-lock) |
11 |
| - |
12 |
| -Reference-counted async lock. |
13 |
| - |
14 |
| -The `Lock` type is similar to `std::sync::Mutex`, except locking is an async operation. |
15 |
| - |
16 |
| -Note that `Lock` by itself acts like an `Arc` in the sense that cloning it returns just |
17 |
| -another reference to the same lock. |
18 |
| - |
19 |
| -Furthermore, `LockGuard` is not tied to `Lock` by a lifetime, so you can keep guards for |
20 |
| -as long as you want. This is useful when you want to spawn a task and move a guard into its |
21 |
| -future. |
22 |
| - |
23 |
| -The locking mechanism uses eventual fairness to ensure locking will be fair on average without |
24 |
| -sacrificing performance. This is done by forcing a fair lock whenever a lock operation is |
25 |
| -starved for longer than 0.5 milliseconds. |
26 |
| - |
27 |
| -## Examples |
28 |
| - |
29 |
| -```rust |
30 |
| -use async_lock::Lock; |
31 |
| -use smol::Task; |
32 |
| - |
33 |
| -let lock = Lock::new(0); |
34 |
| -let mut tasks = vec![]; |
35 |
| - |
36 |
| -for _ in 0..10 { |
37 |
| - let lock = lock.clone(); |
38 |
| - tasks.push(Task::spawn(async move { *lock.lock().await += 1 })); |
39 |
| -} |
40 |
| - |
41 |
| -for task in tasks { |
42 |
| - task.await; |
43 |
| -} |
44 |
| -assert_eq!(*lock.lock().await, 10); |
45 |
| -``` |
| 6 | +https://github.com/stjepang/async-io) |
| 7 | +[]( |
| 8 | +https://crates.io/crates/async-io) |
| 9 | +[]( |
| 10 | +https://docs.rs/async-io) |
46 | 11 |
|
47 | 12 | ## License
|
48 | 13 |
|
|
0 commit comments