Struct multiqueue::Sender
[−]
[src]
pub struct Sender<T: Clone> { /* fields omitted */ }
This class is the sending half of the 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::multiqueue(4); let mut handles = vec![]; for i in 0..2 { // or n let cur_recv = recv.add_stream(); for j in 0..2 { let stream_consumer = cur_recv.clone(); handles.push(thread::spawn(move || { for val in stream_consumer { println!("Stream {} consumer {} got {}", i, j, val); } })); } // cur_recv is dropped here } // 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 // Stream 0 consumer 1 got 2 // Stream 0 consumer 0 got 0 // Stream 1 consumer 0 got 0 // Stream 0 consumer 1 got 1 // Stream 1 consumer 1 got 1 // Stream 1 consumer 0 got 2 // etc
Methods
impl<T: Clone> Sender<T>
[src]
fn try_send(&self, val: T) -> Result<(), TrySendError<T>>
fn unsubscribe(self)
Removes the writer as a producer to the queue
Trait Implementations
impl<T: Clone> Clone for Sender<T>
[src]
fn clone(&self) -> Sender<T>
Clones the writer, allowing multiple writers to push into the queue from different threads
Examples
use multiqueue::multiqueue; let (writer, reader) = multiqueue(16); let writer2 = writer.clone(); writer.try_send(1).unwrap(); writer2.try_send(2).unwrap(); assert_eq!(1, reader.try_recv().unwrap()); assert_eq!(2, reader.try_recv().unwrap());
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more