|
1 |
| -# async-mutex |
| 1 | +# concurrent-queue |
2 | 2 |
|
3 |
| -[]( |
4 |
| -https://github.com/stjepang/async-mutex/actions) |
| 3 | +[]( |
| 4 | +https://github.com/stjepang/concurrent-queue/actions) |
5 | 5 | [](
|
6 |
| -https://github.com/stjepang/async-mutex) |
7 |
| -[]( |
8 |
| -https://crates.io/crates/async-mutex) |
9 |
| -[]( |
10 |
| -https://docs.rs/async-mutex) |
| 6 | +https://github.com/stjepang/concurrent-queue) |
| 7 | +[]( |
| 8 | +https://crates.io/crates/concurrent-queue) |
| 9 | +[]( |
| 10 | +https://docs.rs/concurrent-queue) |
11 | 11 |
|
12 |
| -An async mutex. |
| 12 | +A concurrent multi-producer multi-consumer queue. |
13 | 13 |
|
14 |
| -The locking mechanism uses eventual fairness to ensure locking will be fair on average without |
15 |
| -sacrificing performance. This is done by forcing a fair lock whenever a lock operation is |
16 |
| -starved for longer than 0.5 milliseconds. |
| 14 | +There are two kinds of queues: |
| 15 | + |
| 16 | +1. Bounded queue with limited capacity. |
| 17 | +2. Unbounded queue with unlimited capacity. |
| 18 | + |
| 19 | +Queues also have the capability to get closed at any point. When closed, no more items can be |
| 20 | +pushed into the queue, although the remaining items can still be popped. |
| 21 | + |
| 22 | +These features make it easy to build channels similar to `std::sync::mpsc` on top of this |
| 23 | +crate. |
17 | 24 |
|
18 | 25 | ## Examples
|
19 | 26 |
|
20 | 27 | ```rust
|
21 |
| -use async_mutex::Mutex; |
22 |
| -use smol::Task; |
23 |
| -use std::sync::Arc; |
24 |
| - |
25 |
| -let m = Arc::new(Mutex::new(0)); |
26 |
| -let mut tasks = vec![]; |
27 |
| - |
28 |
| -for _ in 0..10 { |
29 |
| - let m = m.clone(); |
30 |
| - tasks.push(Task::spawn(async move { |
31 |
| - *m.lock().await += 1; |
32 |
| - })); |
33 |
| -} |
34 |
| - |
35 |
| -for t in tasks { |
36 |
| - t.await; |
37 |
| -} |
38 |
| -assert_eq!(*m.lock().await, 10); |
| 28 | +use concurrent_queue::ConcurrentQueue; |
| 29 | + |
| 30 | +let q = ConcurrentQueue::unbounded(); |
| 31 | +q.push(1).unwrap(); |
| 32 | +q.push(2).unwrap(); |
| 33 | + |
| 34 | +assert_eq!(q.pop(), Ok(1)); |
| 35 | +assert_eq!(q.pop(), Ok(2)); |
39 | 36 | ```
|
40 | 37 |
|
41 | 38 | ## License
|
|
0 commit comments