Module st3::fifo

source ·
Expand description

FIFO, bounded, work-stealing queue.

Example

use std::thread;
use st3::fifo::Worker;

// Push 4 items into a FIFO queue of capacity 256.
let worker = Worker::new(256);
worker.push("a").unwrap();
worker.push("b").unwrap();
worker.push("c").unwrap();
worker.push("d").unwrap();

// Steal items concurrently.
let stealer = worker.stealer();
let th = thread::spawn(move || {
    let other_worker = Worker::new(256);

    // Try to steal half the items and return the actual count of stolen items.
    match stealer.steal(&other_worker, |n| n/2) {
        Ok(actual) => actual,
        Err(_) => 0,
    }
});

// Pop items concurrently.
let mut pop_count = 0;
while worker.pop().is_some() {
    pop_count += 1;
}

// Does it add up?
let steal_count = th.join().unwrap();
assert_eq!(pop_count + steal_count, 4);

Structs

A draining iterator for Worker<T>.
Handle for multi-threaded stealing operations.
Handle for single-threaded FIFO push and pop operations.