Skip to content

JuliusEmperorOfRome/concurrent_qs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

concurrent_qs

A rust crate that aims to provide access to common queues used in concurrent programming. Currently, only SPSC queues are provided.

Usage

All queues are enabled with a {type}-{bounded|unbounded} feature and are placed in {type}::{bounded|unbounded}. There are also {type}-all and full features, enabling all {type} queues and the full library respectively.

For example, to use a bounded SPSC queue, you would write something like this.

[dependencies]
concurrent_qs = ["spsc-bounded"]
use concurrent_qs::spsc::bounded;
use concurrent_qs::error::*;

fn main() {
    let(tx, rx) = bounded::channel::<&'static str>(1);

    std::thread::spawn(move || {
        tx.send("Hello").expect("Fails only after 'rx' is dropped.");
        tx.send(", ").unwrap();
        tx.send("World").unwrap();
        // Do not use try_send this way. If you **need** to send the value
        // to proceed, use send instead.
        let mut send: &'static str = "!";
        loop {
            send = match tx.try_send(send) {
                Ok(()) => return,
                Err(TrySendError::Full(fail)) => fail,
                Err(TrySendError::Disconnected(_)) => unreachable!(),
            }
        }
    });

    let mut result = String::new();
    result.push_str(
        loop {
            // Do not use try_recv this way. If you **need** to
            // get a value to proceed, use recv instead.
            match rx.try_recv() {
                Ok(s) => break s,
                Err(TryRecvError::Empty) => std::hint::spin_loop(),
                Err(TryRecvError::Disconnected) => {
                    unreachable!("Disconnect only happens after receiving all data")
                }
            }
        }
    );
    while let Ok(s) = rx.recv() {
        result.push_str(s);
    }

    assert_eq!(result.as_str(), "Hello, World!");
}

Features

  • full — enables all queues in the crate.
  • spsc-all — enables [un]bounded::spsc queues.
  • spsc-bounded — enables the bounded::spsc queue.
  • spsc-unbounded — enables the unbounded::spsc queue.

About

A crate of concurrent queues

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages