[−][src]Struct crossbeam_deque::Deque
A concurrent work-stealing deque.
A deque has two ends: bottom and top. Elements can be push
ed into the bottom and pop
ped
from the bottom. The top end is special in that elements can only be stolen from it using the
steal
method.
Stealers
While Deque
doesn't implement Sync
, it can create Stealer
s using the method
stealer
, and those can be easily shared among multiple threads. Stealer
s can
only steal
elements from the top end of the deque.
Capacity
The data structure is dynamically grows as elements are inserted and removed from it. If the internal buffer gets full, a new one twice the size of the original is allocated. Similarly, if it is less than a quarter full, a new buffer half the size of the original is allocated.
In order to prevent frequent resizing (reallocations may be costly), it is possible to specify
a large minimum capacity for the deque by calling Deque::with_min_capacity
. This
constructor will make sure that the internal buffer never shrinks below that size.
Examples
use crossbeam_deque::{Deque, Steal}; let d = Deque::with_min_capacity(1000); let s = d.stealer(); d.push('a'); d.push('b'); d.push('c'); assert_eq!(d.pop(), Some('c')); assert_eq!(d.steal(), Steal::Data('a')); assert_eq!(s.steal(), Steal::Data('b'));
Methods
impl<T> Deque<T>
[src]
impl<T> Deque<T>
pub fn new() -> Deque<T>
[src]
pub fn new() -> Deque<T>
Returns a new deque.
The internal buffer is destructed as soon as the deque and all its stealers get dropped.
Examples
use crossbeam_deque::Deque; let d = Deque::<i32>::new();
pub fn with_min_capacity(min_cap: usize) -> Deque<T>
[src]
pub fn with_min_capacity(min_cap: usize) -> Deque<T>
Returns a new deque with the specified minimum capacity.
If the capacity is not a power of two, it will be rounded up to the next one.
Examples
use crossbeam_deque::Deque; // The minimum capacity will be rounded up to 1024. let d = Deque::<i32>::with_min_capacity(1000);
pub fn is_empty(&self) -> bool
[src]
pub fn is_empty(&self) -> bool
Returns true
if the deque is empty.
Examples
use crossbeam_deque::Deque; let d = Deque::new(); assert!(d.is_empty()); d.push("foo"); assert!(!d.is_empty());
pub fn len(&self) -> usize
[src]
pub fn len(&self) -> usize
Returns the number of elements in the deque.
Examples
use crossbeam_deque::Deque; let d = Deque::new(); d.push('a'); d.push('b'); d.push('c'); assert_eq!(d.len(), 3);
pub fn push(&self, value: T)
[src]
pub fn push(&self, value: T)
Pushes an element into the bottom of the deque.
If the internal buffer is full, a new one twice the capacity of the current one will be allocated.
Examples
use crossbeam_deque::Deque; let d = Deque::new(); d.push(1); d.push(2);
pub fn pop(&self) -> Option<T>
[src]
pub fn pop(&self) -> Option<T>
Pops an element from the bottom of the deque.
If the internal buffer is less than a quarter full, a new buffer half the capacity of the current one will be allocated.
Examples
use crossbeam_deque::Deque; let d = Deque::new(); d.push(1); d.push(2); assert_eq!(d.pop(), Some(2)); assert_eq!(d.pop(), Some(1)); assert_eq!(d.pop(), None);
pub fn steal(&self) -> Steal<T>
[src]
pub fn steal(&self) -> Steal<T>
Steals an element from the top of the deque.
Unlike most methods in concurrent data structures, if another operation gets in the way
while attempting to steal data, this method will return immediately with Steal::Retry
instead of retrying.
If the internal buffer is less than a quarter full, a new buffer half the capacity of the current one will be allocated.
Examples
use crossbeam_deque::{Deque, Steal}; let d = Deque::new(); d.push(1); d.push(2); // Attempt to steal an element. // // No other threads are working with the deque, so this time we know for sure that we // won't get `Steal::Retry` as the result. assert_eq!(d.steal(), Steal::Data(1)); // Attempt to steal an element, but keep retrying if we get `Retry`. loop { match d.steal() { Steal::Empty => panic!("should steal something"), Steal::Data(data) => { assert_eq!(data, 2); break; } Steal::Retry => {} } }
pub fn stealer(&self) -> Stealer<T>
[src]
pub fn stealer(&self) -> Stealer<T>
Creates a stealer that can be shared with other threads.
Examples
use crossbeam_deque::{Deque, Steal}; use std::thread; let d = Deque::new(); d.push(1); d.push(2); let s = d.stealer(); thread::spawn(move || { assert_eq!(s.steal(), Steal::Data(1)); }).join().unwrap();
Trait Implementations
impl<T: Send> Send for Deque<T>
[src]
impl<T: Send> Send for Deque<T>
impl<T> Default for Deque<T>
[src]
impl<T> Default for Deque<T>
impl<T> Debug for Deque<T>
[src]
impl<T> Debug for Deque<T>
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T> From for T
[src]
impl<T> From for T
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
try_from
)Performs the conversion.
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
try_from
)Performs the conversion.
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Gets the TypeId
of self
. Read more