Struct MPMCSender

Source
pub struct MPMCSender<T> { /* private fields */ }
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) = multiqueue2::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
// etc

Implementations§

Source§

impl<T> MPMCSender<T>

Source

pub fn try_send(&self, val: T) -> Result<(), TrySendError<T>>

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))

Source

pub fn unsubscribe(self)

Removes this writer from the queue

Trait Implementations§

Source§

impl<T: Clone> Clone for MPMCSender<T>

Source§

fn clone(&self) -> MPMCSender<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Send> Send for MPMCSender<T>

Auto Trait Implementations§

§

impl<T> !Freeze for MPMCSender<T>

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.