A high-performance multi-producer multi-consumer (MPMC) queue implementation based on Tokio.
- Asynchronous implementation based on Tokio
- Support for multi-producer multi-consumer pattern
- Message processing using consumer pool
- Simple and intuitive API
- Complete error handling
- Queue capacity control
Add the following dependency to your Cargo.toml
:
[dependencies]
tokio-mpmc = "0.1"
use tokio_mpmc::Queue;
#[tokio::main]
async fn main() {
// Create a queue with capacity of 100
let queue = Queue::new(100);
// Send a message
if let Err(e) = queue.send("Hello").await {
eprintln!("Send failed: {}", e);
}
// Receive a message
match queue.receive().await {
Ok(Some(msg)) => println!("Received message: {}", msg),
Ok(None) => println!("Queue is empty"),
Err(e) => eprintln!("Receive failed: {}", e),
}
// Close the queue
queue.close().await;
}
Main APIs include:
Queue::new(capacity)
- Create a new queueQueue::send(value)
- Send a messageQueue::receive()
- Receive a messageQueue::close()
- Close the queueQueue::len()
- Get queue lengthQueue::is_empty()
- Check if queue is emptyQueue::is_full()
- Check if queue is fullQueue::is_closed()
- Check if queue is closed
This project is licensed under the Apache-2.0 License. See the LICENSE file for details.