Crates.io | delay-queue |
lib.rs | delay-queue |
version | 0.2.0 |
source | src |
created_at | 2017-12-09 17:38:54.737875 |
updated_at | 2017-12-23 20:39:12.875062 |
description | A concurrent unbounded blocking queue where each element can only be removed when its delay expires. |
homepage | |
repository | https://github.com/tbcardoso/rust-delay-queue |
max_upload_size | |
id | 42257 |
size | 45,687 |
A concurrent unbounded blocking queue where each element can only be removed when its delay expires.
extern crate delay_queue;
use std::time::{Duration, Instant};
use std::thread;
use delay_queue::{Delay, DelayQueue};
fn main() {
let queue: DelayQueue<Delay<&str>> = DelayQueue::new();
// Clone the queue and move it to the consumer thread
let mut consumer_queue = queue.clone();
let consumer_handle = thread::spawn(move || {
// The pop() will block until an item is available and its delay has expired
println!("First pop: {}", consumer_queue.pop().value);
println!("Second pop: {}", consumer_queue.pop().value);
});
// Clone the queue and move it to the producer thread
let mut producer_queue = queue.clone();
let producer_handle = thread::spawn(move || {
// This item can only be popped after 3 seconds have passed
producer_queue.push(Delay::for_duration("3s", Duration::from_secs(3)));
// This item can be popped immediately
producer_queue.push(Delay::until_instant("now", Instant::now()));
});
consumer_handle.join().unwrap();
producer_handle.join().unwrap();
assert!(queue.is_empty());
}
You can run this example with the command cargo run --example basic_usage
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.