Struct multiqueue::MPMCSender [−][src]
pub struct MPMCSender<T> { /* fields omitted */ }Expand description
This class is the sending half of the mpmc MultiQueue. It supports both
single and multi consumer modes with competitive performance in each case.
It only supports nonblocking writes (the futures sender being an exception)
as well as being the conduit for adding new writers.
Examples
use std::thread;
let (send, recv) = multiqueue::mpmc_queue(4);
let mut handles = vec![];
for i in 0..2 { // or n
let consumer = recv.clone();
handles.push(thread::spawn(move || {
for val in consumer {
println!("Consumer {} got {}", i, val);
}
}));
}
// Take notice that I drop the reader - this removes it from
// the queue, meaning that the readers in the new threads
// won't get starved by the lack of progress from recv
recv.unsubscribe();
for i in 0..10 {
// Don't do this busy loop in real stuff unless you're really sure
loop {
if send.try_send(i).is_ok() {
break;
}
}
}
drop(send);
for t in handles {
t.join();
}
// prints along the lines of
// Consumer 1 got 2
// Consumer 0 got 0
// Consumer 0 got 1
// etcImplementations
Tries to send a value into the queue
If there is no space, returns Err(TrySendError::Full(val))
If there are no readers, returns Err(TrySendError::Disconnected(val))
Removes this writer from the queue
Trait Implementations
Auto Trait Implementations
impl<T> !RefUnwindSafe for MPMCSender<T>
impl<T> !Sync for MPMCSender<T>
impl<T> Unpin for MPMCSender<T>
impl<T> !UnwindSafe for MPMCSender<T>
Blanket Implementations
Mutably borrows from an owned value. Read more
