pub struct LockFreeStack<T> { /* private fields */ }Expand description
A lock-free stack implementation using Treiber’s algorithm
This stack provides:
- Wait-free push operations
- Lock-free pop operations
- ABA problem prevention through proper memory management
- Performance metrics collection
§Type Parameters
T- The type of elements stored in the stack
§Examples
use velocityx::stack::LockFreeStack;
let stack = LockFreeStack::new();
// Push elements
stack.push(1);
stack.push(2);
stack.push(3);
// Pop elements
assert_eq!(stack.pop(), Some(3));
assert_eq!(stack.pop(), Some(2));
assert_eq!(stack.pop(), Some(1));
assert_eq!(stack.pop(), None);Implementations§
Source§impl<T> LockFreeStack<T>
impl<T> LockFreeStack<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty lock-free stack
§Examples
use velocityx::stack::LockFreeStack;
let stack: LockFreeStack<i32> = LockFreeStack::new();Sourcepub fn pop(&self) -> Option<T>
pub fn pop(&self) -> Option<T>
Pop a value from the stack
This operation is lock-free and will complete if other threads make progress.
§Returns
Some(value)if the stack was not emptyNoneif the stack was empty
§Examples
use velocityx::stack::LockFreeStack;
let stack = LockFreeStack::new();
stack.push(42);
let value = stack.pop();
assert_eq!(value, Some(42));Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the stack is empty
This operation is lock-free and provides a snapshot of the stack’s state.
§Returns
true if the stack is empty, false otherwise
§Examples
use velocityx::stack::LockFreeStack;
let stack = LockFreeStack::new();
assert!(stack.is_empty());
stack.push(42);
assert!(!stack.is_empty());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the approximate number of elements in the stack
Note: This is an expensive operation as it traverses the entire stack. Use only for debugging or monitoring purposes.
§Returns
The approximate number of elements in the stack
§Examples
use velocityx::stack::LockFreeStack;
let stack = LockFreeStack::new();
assert_eq!(stack.len(), 0);
stack.push(1);
stack.push(2);
assert_eq!(stack.len(), 2);Sourcepub fn pop_batch(&self, max_count: usize) -> Vec<T>
pub fn pop_batch(&self, max_count: usize) -> Vec<T>
Try to pop multiple elements at once
This can be more efficient than individual pops as it reduces atomic operations.
§Arguments
max_count- Maximum number of elements to pop
§Returns
A vector containing the popped elements (in LIFO order)
§Examples
use velocityx::stack::LockFreeStack;
let stack = LockFreeStack::new();
stack.push(1);
stack.push(2);
stack.push(3);
let elements = stack.pop_batch(2);
assert_eq!(elements.len(), 2);Sourcepub fn push_batch<I>(&self, values: I)where
I: IntoIterator<Item = T>,
pub fn push_batch<I>(&self, values: I)where
I: IntoIterator<Item = T>,
Push multiple elements at once
Elements are pushed in the order they appear in the iterator. The last element will be at the top of the stack.
§Arguments
values- Iterator of values to push
§Examples
use velocityx::stack::LockFreeStack;
let stack = LockFreeStack::new();
let values = vec![1, 2, 3];
stack.push_batch(values);
assert_eq!(stack.pop(), Some(3));
assert_eq!(stack.pop(), Some(2));
assert_eq!(stack.pop(), Some(1));Trait Implementations§
Source§impl<T: Debug> Debug for LockFreeStack<T>
impl<T: Debug> Debug for LockFreeStack<T>
Source§impl<T> Default for LockFreeStack<T>
impl<T> Default for LockFreeStack<T>
Source§impl<T> Drop for LockFreeStack<T>
impl<T> Drop for LockFreeStack<T>
Source§impl<T> MetricsCollector for LockFreeStack<T>
Available on crate feature std only.
impl<T> MetricsCollector for LockFreeStack<T>
std only.