Struct lib_wc::executors::RayonThreadPool
source · pub struct RayonThreadPool { /* private fields */ }
Expand description
Thin wrapper of rayon::ThreadPool which allows the use of ThreadPool::shutdown
methpd
Trait Implementations§
source§impl ThreadPool for RayonThreadPool
impl ThreadPool for RayonThreadPool
source§fn new(threads: usize) -> Result<Self>
fn new(threads: usize) -> Result<Self>
Create a new thread pool with the given number of threads
Examples
use lib_wc::executors::{RayonThreadPool, ThreadPool};
let tp = RayonThreadPool::new(4).unwrap();
source§fn spawn<F>(&self, job: F)where
F: FnOnce() + Send + 'static,
fn spawn<F>(&self, job: F)where F: FnOnce() + Send + 'static,
Spawn a new task on the thread pool
Examples
use lib_wc::executors::{RayonThreadPool, ThreadPool};
let tp = RayonThreadPool::new(4).unwrap();
tp.spawn(|| {
println!("Hello from a thread!");
});
source§fn shutdown(self)
fn shutdown(self)
Wait for all currently running tasks to complete
Examples
use lib_wc::executors::{RayonThreadPool, ThreadPool};
use std::sync::atomic::{AtomicUsize, Ordering};
static ATOMIC_COUNTER: AtomicUsize = AtomicUsize::new(0);
let tp = RayonThreadPool::new(4).unwrap();
for _ in 0..100 {
tp.spawn(|| {
ATOMIC_COUNTER.fetch_add(1, Ordering::Acquire);
});
}
tp.shutdown();
assert_eq!(ATOMIC_COUNTER.load(Ordering::Relaxed), 100);