Struct cordyceps::mpsc_queue::OwnedConsumer

source ·
pub struct OwnedConsumer<T: Linked<Links<T>>> { /* private fields */ }
Available on crate feature alloc only.
Expand description

An owned handle that holds the right to dequeue elements from the queue.

This can be used when one thread wishes to dequeue many elements at a time, to avoid the overhead of ensuring mutual exclusion on every dequeue or try_dequeue call.

This type is returned by the MpscQueue::consume_owned and MpscQueue::try_consume_owned methods.

This is similar to the Consumer type, but the queue is stored in an Arc rather than borrowed. This allows a single OwnedConsumer instance to be stored in a struct and used indefinitely.

Since the queue is stored in an Arc, this requires the alloc feature flag to be enabled.

Implementations§

source§

impl<T: Linked<Links<T>>> OwnedConsumer<T>

source

pub fn dequeue(&self) -> Option<T::Handle>

Dequeue an element from the queue.

As discussed in the algorithm description on 1024cores.net, it is possible for this queue design to enter an inconsistent state if the consumer tries to dequeue an element while a producer is in the middle of enqueueing a new element. If this occurs, the consumer must briefly wait before dequeueing an element. This method will wait by spinning with an exponential backoff if the queue is in an inconsistent state.

The Consumer::try_dequeue will return an error rather than waiting when the queue is in an inconsistent state.

§Returns
  • Some(T::Handle) if an element was successfully dequeued
  • None if the queue is empty
source

pub fn try_dequeue(&self) -> Result<T::Handle, TryDequeueError>

Try to dequeue an element from the queue, without waiting if the queue is in an inconsistent state.

As discussed in the algorithm description on 1024cores.net, it is possible for this queue design to enter an inconsistent state if the consumer tries to dequeue an element while a producer is in the middle of enqueueing a new element. If this occurs, the consumer must briefly wait before dequeueing an element. This method returns TryDequeueError::Inconsistent when the queue is in an inconsistent state.

The Consumer::dequeue method will instead wait (by spinning with an exponential backoff) when the queue is in an inconsistent state.

§Returns
§Returns
  • Some(T::Handle) if an element was successfully dequeued
  • None if the queue is empty
source

pub fn has_producers(&self) -> bool

Returns true if any producers exist for this queue.

Trait Implementations§

source§

impl<T: Linked<Links<T>>> Debug for OwnedConsumer<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Linked<Links<T>>> Drop for OwnedConsumer<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for OwnedConsumer<T>

§

impl<T> !RefUnwindSafe for OwnedConsumer<T>

§

impl<T> Send for OwnedConsumer<T>
where T: Send, <T as Linked<Links<T>>>::Handle: Send,

§

impl<T> Sync for OwnedConsumer<T>
where T: Send, <T as Linked<Links<T>>>::Handle: Send,

§

impl<T> Unpin for OwnedConsumer<T>

§

impl<T> !UnwindSafe for OwnedConsumer<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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.