#concurrency #delay #queue #wait #schedule

delay-queue

A concurrent unbounded blocking queue where each element can only be removed when its delay expires

2 unstable releases

Uses old Rust 2015

0.2.0 Dec 23, 2017
0.1.0 Dec 9, 2017

#5 in #wait

Download history 9/week @ 2021-06-28 10/week @ 2021-07-05 6/week @ 2021-07-12 14/week @ 2021-07-19 6/week @ 2021-07-26 11/week @ 2021-08-02 8/week @ 2021-08-09 3/week @ 2021-08-16 9/week @ 2021-08-23 4/week @ 2021-09-06 54/week @ 2021-09-13 5/week @ 2021-09-20 12/week @ 2021-09-27 5/week @ 2021-10-04 9/week @ 2021-10-11

239 downloads per month
Used in 2 crates

MIT/Apache

31KB
458 lines

DelayQueue for Rust

Build Status Build status Crates.io docs.rs

A concurrent unbounded blocking queue where each element can only be removed when its delay expires.

Example

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

License

Licensed under either of

at your option.

Contribution

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.

No runtime deps