pub struct LockFreePool<T> { /* private fields */ }Available on crate features
std and lock-free only.Expand description
A lock-free memory pool using atomic operations.
This pool provides better performance under high contention compared
to ThreadSafePool by avoiding locks. Requires the lock-free feature.
§Examples
use fastalloc::LockFreePool;
use std::sync::Arc;
use std::thread;
let pool = Arc::new(LockFreePool::<i32>::new(1000).unwrap());
let mut handles = vec![];
for i in 0..8 {
let pool_clone = Arc::clone(&pool);
handles.push(thread::spawn(move || {
for j in 0..100 {
let _handle = pool_clone.allocate(i * 100 + j).unwrap();
}
}));
}
for handle in handles {
handle.join().unwrap();
}Implementations§
Source§impl<T> LockFreePool<T>
impl<T> LockFreePool<T>
Sourcepub fn new(capacity: usize) -> Result<Self>
pub fn new(capacity: usize) -> Result<Self>
Creates a new lock-free pool with the specified capacity.
Note: The current implementation is a simplified version. A full production implementation would use a more sophisticated lock-free data structure.
Sourcepub fn with_initializer<F>(capacity: usize, init: F) -> Result<Self>where
F: FnMut() -> T,
pub fn with_initializer<F>(capacity: usize, init: F) -> Result<Self>where
F: FnMut() -> T,
Pre-populates the pool with objects created by the initializer.
Sourcepub fn try_allocate(&self) -> Option<Box<T>>
pub fn try_allocate(&self) -> Option<Box<T>>
Attempts to allocate an object from the pool.
If the pool is empty, this will fail. Unlike other pool types, this simplified lock-free implementation does not automatically grow.
Sourcepub fn return_object(&self, object: Box<T>)
pub fn return_object(&self, object: Box<T>)
Returns an object to the pool.
Trait Implementations§
Source§impl<T> Clone for LockFreePool<T>
impl<T> Clone for LockFreePool<T>
impl<T: Send> Send for LockFreePool<T>
impl<T: Send> Sync for LockFreePool<T>
Auto Trait Implementations§
impl<T> !Freeze for LockFreePool<T>
impl<T> RefUnwindSafe for LockFreePool<T>
impl<T> Unpin for LockFreePool<T>
impl<T> UnwindSafe for LockFreePool<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more