Expand description

Magnetic contains a set of high-performance queues useful for developing low-latency applications. All queues are FIFO unless otherwise specified.

Examples

use std::thread::spawn;
use magnetic::spsc::spsc_queue;
use magnetic::buffer::dynamic::DynamicBuffer;
use magnetic::{Producer, Consumer};

let (p, c) = spsc_queue(DynamicBuffer::new(32).unwrap());

// Push and pop within a single thread
p.push(1).unwrap();
assert_eq!(c.pop(), Ok(1));

// Push and pop from multiple threads. Since this example is using the
// SPSC queue, only one producer and one consumer are allowed.
let t1 = spawn(move || {
    for i in 0..10 {
        println!("Producing {}", i);
        p.push(i).unwrap();
    }
    p
});

let t2 = spawn(move || {
    loop {
        let i = c.pop().unwrap();
        println!("Consumed {}", i);
        if i == 9 { break; }
    }
});

t1.join().unwrap();
t2.join().unwrap();

Modules

Memory buffer API

Multiple-producer multiple-consumer queue

Multiple-producer single-consumer queue

Single-producer multiple-consumer queue

Single-producer single-consumer queue

Enums

Possible errors for Consumer::pop

Possible errors for Producer::push

Possible errors for Consumer::try_pop

Possible errors for Producer::try_push

Traits

The consumer end of the queue allows for receiving data. Consumer<T> is always Send, but is only Sync for multi-consumer (SPMC, MPMC) queues.

The consumer end of the queue allows for sending data. Producer<T> is always Send, but is only Sync for multi-producer (MPSC, MPMC) queues.