|
1 | 1 | # lf-queue |
| 2 | + |
| 3 | +[](https://crates.io/crates/lf-queue) |
| 4 | +[](https://docs.rs/lf-queue) |
| 5 | +[](https://github.com/broucz/lf-queue/actions/workflows/ci.yml?query=branch%3Amain) |
| 6 | +[](LICENSE) |
| 7 | + |
2 | 8 | A lock-free multi-producer multi-consumer unbounded queue. |
| 9 | + |
| 10 | +## Examples |
| 11 | + |
| 12 | +```toml |
| 13 | +[dependencies] |
| 14 | +lf-queue = "0.1" |
| 15 | +``` |
| 16 | + |
| 17 | +Single Producer - Single Consumer: |
| 18 | + |
| 19 | +```rust |
| 20 | +use lf_queue::Queue; |
| 21 | + |
| 22 | +fn main() { |
| 23 | + const COUNT: usize = 1_000; |
| 24 | + let queue: Queue<usize> = Queue::new(); |
| 25 | + |
| 26 | + for i in 0..COUNT { |
| 27 | + queue.push(i); |
| 28 | + } |
| 29 | + |
| 30 | + for i in 0..COUNT { |
| 31 | + assert_eq!(i, queue.pop().unwrap()); |
| 32 | + } |
| 33 | + |
| 34 | + assert!(queue.pop().is_none()); |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Multi Producer - Single Consumer: |
| 39 | + |
| 40 | +```rust |
| 41 | +use lf_queue::Queue; |
| 42 | +use std::thread; |
| 43 | + |
| 44 | +fn main() { |
| 45 | + const COUNT: usize = 1_000; |
| 46 | + const CONCURRENCY: usize = 4; |
| 47 | + |
| 48 | + let queue: Queue<usize> = Queue::new(); |
| 49 | + |
| 50 | + let ths: Vec<_> = (0..CONCURRENCY) |
| 51 | + .map(|_| { |
| 52 | + let q = queue.clone(); |
| 53 | + thread::spawn(move || { |
| 54 | + for i in 0..COUNT { |
| 55 | + q.push(i); |
| 56 | + } |
| 57 | + }) |
| 58 | + }) |
| 59 | + .collect(); |
| 60 | + |
| 61 | + for th in ths { |
| 62 | + th.join().unwrap(); |
| 63 | + } |
| 64 | + |
| 65 | + for _ in 0..COUNT * CONCURRENCY { |
| 66 | + assert!(queue.pop().is_some()); |
| 67 | + } |
| 68 | + |
| 69 | + assert!(queue.pop().is_none()); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Single Producer - Multi Consumer: |
| 74 | + |
| 75 | +```rust |
| 76 | +use lf_queue::Queue; |
| 77 | +use std::thread; |
| 78 | + |
| 79 | +fn main() { |
| 80 | + const COUNT: usize = 1_000; |
| 81 | + const CONCURRENCY: usize = 4; |
| 82 | + |
| 83 | + let queue: Queue<usize> = Queue::new(); |
| 84 | + |
| 85 | + for i in 0..COUNT * CONCURRENCY { |
| 86 | + queue.push(i); |
| 87 | + } |
| 88 | + |
| 89 | + let ths: Vec<_> = (0..CONCURRENCY) |
| 90 | + .map(|_| { |
| 91 | + let q = queue.clone(); |
| 92 | + thread::spawn(move || { |
| 93 | + for _ in 0..COUNT { |
| 94 | + loop { |
| 95 | + if q.pop().is_some() { |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + }) |
| 101 | + }) |
| 102 | + .collect(); |
| 103 | + |
| 104 | + for th in ths { |
| 105 | + th.join().unwrap(); |
| 106 | + } |
| 107 | + |
| 108 | + assert!(queue.pop().is_none()); |
| 109 | +} |
| 110 | + |
| 111 | +``` |
| 112 | + |
| 113 | +Multi Producer - Multi Consumer: |
| 114 | + |
| 115 | +```rust |
| 116 | +use lf_queue::Queue; |
| 117 | +use std::sync::atomic::{AtomicUsize, Ordering}; |
| 118 | +use std::sync::Arc; |
| 119 | +use std::thread; |
| 120 | + |
| 121 | +fn main() { |
| 122 | + const COUNT: usize = 1_000; |
| 123 | + const CONCURRENCY: usize = 4; |
| 124 | + |
| 125 | + let queue: Queue<usize> = Queue::new(); |
| 126 | + let items = Arc::new((0..COUNT).map(|_| AtomicUsize::new(0)).collect::<Vec<_>>()); |
| 127 | + |
| 128 | + let ths: Vec<_> = (0..CONCURRENCY) |
| 129 | + .map(|_| { |
| 130 | + let q = queue.clone(); |
| 131 | + let its = items.clone(); |
| 132 | + thread::spawn(move || { |
| 133 | + for _ in 0..COUNT { |
| 134 | + let n = loop { |
| 135 | + if let Some(x) = q.pop() { |
| 136 | + break x; |
| 137 | + } else { |
| 138 | + thread::yield_now(); |
| 139 | + } |
| 140 | + }; |
| 141 | + its[n].fetch_add(1, Ordering::SeqCst); |
| 142 | + } |
| 143 | + }) |
| 144 | + }) |
| 145 | + .map(|_| { |
| 146 | + let q = queue.clone(); |
| 147 | + thread::spawn(move || { |
| 148 | + for i in 0..COUNT { |
| 149 | + q.push(i); |
| 150 | + } |
| 151 | + }) |
| 152 | + }) |
| 153 | + .collect(); |
| 154 | + |
| 155 | + for th in ths { |
| 156 | + th.join().unwrap(); |
| 157 | + } |
| 158 | + |
| 159 | + thread::sleep(std::time::Duration::from_millis(10)); |
| 160 | + |
| 161 | + for c in &*items { |
| 162 | + assert_eq!(c.load(Ordering::SeqCst), CONCURRENCY); |
| 163 | + } |
| 164 | + |
| 165 | + assert!(queue.pop().is_none()); |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +## Acknowledgement |
| 170 | + |
| 171 | +This implementation of a lock-free queue in Rust took inspiration from the [`concurrent-queue`](https://github.com/smol-rs/concurrent-queue) crate and aims to be used for educational purposes. The code documentation help you to discover the algorithm used to implement a concurrent lock-free queue in Rust, but might not yet be beginner-friendly. More details and learning materials will be added over time. |
| 172 | + |
| 173 | +## License |
| 174 | + |
| 175 | +This project is licensed under the [MIT license](LICENSE). |
| 176 | + |
| 177 | +## Contribution |
| 178 | + |
| 179 | +Unless you explicitly state otherwise, any contribution intentionally submitted |
| 180 | +for inclusion in the work by you, shall be licensed as above, without any additional |
| 181 | +terms or conditions. |
| 182 | + |
| 183 | +Note that, as of now, my focus is on improving the documentation of this crate, not adding any additional feature. Please open an issue and start a discussion before working on any significant PR. |
| 184 | + |
| 185 | +Contributions are welcome. |
0 commit comments