pub struct ThreadPool { /* private fields */ }Expand description
Simple thread pool to allow benchmarks to run on pre-warmed threads instead of creating new threads for every batch of iterations. Thread reuse reduces benchmark harness overhead.
Threads in the pool are bound to specific processors according to the provided processor set, ensuring consistent processor affinity across benchmark runs.
§Examples
use many_cpus::ProcessorSet;
use new_zealand::nz;
use par_bench::ThreadPool;
// Create a thread pool using the default processor set
let pool = ThreadPool::new(&ProcessorSet::default());
println!("Default pool has {} threads", pool.thread_count());
// Create a thread pool with a specific processor set
if let Some(processors) = ProcessorSet::builder().take(nz!(2)) {
let pool = ThreadPool::new(&processors);
assert_eq!(pool.thread_count().get(), 2);
}§Lifecycle
Dropping the pool will wait for all threads to finish executing their tasks.
Implementations§
Source§impl ThreadPool
impl ThreadPool
Sourcepub fn new(processors: &ProcessorSet) -> Self
pub fn new(processors: &ProcessorSet) -> Self
Creates a thread pool with one thread per processor in the provided processor set.
Each thread will be bound to its corresponding processor, ensuring consistent processor affinity for benchmark execution.
§Examples
use many_cpus::ProcessorSet;
use new_zealand::nz;
use par_bench::ThreadPool;
// Create pool with specific processors
if let Some(processors) = ProcessorSet::builder().take(nz!(4)) {
let pool = ThreadPool::new(&processors);
assert_eq!(pool.thread_count().get(), 4);
}Sourcepub fn thread_count(&self) -> NonZero<usize>
pub fn thread_count(&self) -> NonZero<usize>
Returns the number of threads in the pool.
This is always equal to the number of processors in the processor set used to create the pool.
§Examples
use many_cpus::ProcessorSet;
use par_bench::ThreadPool;
let pool = ThreadPool::new(&ProcessorSet::default());
println!("Pool has {} worker threads", pool.thread_count());Trait Implementations§
Source§impl Debug for ThreadPool
impl Debug for ThreadPool
Auto Trait Implementations§
impl Freeze for ThreadPool
impl !RefUnwindSafe for ThreadPool
impl Send for ThreadPool
impl Sync for ThreadPool
impl Unpin for ThreadPool
impl !UnwindSafe for ThreadPool
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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more