Struct std::thread::pool::ThreadPool
A thread pool.
Example
use std::thread::pool::ThreadPool;
use std::sync::Atomic;
// Use 10 threads
let pool = ThreadPool::unbounded(10);
let counter = Atomic::new(0);
for _ in 0..1000 {
pool
.spawn_detached(|&counter| {
counter.fetch_add(1, sync::Ordering::Relaxed);
})
.unwrap();
}
// Wait for all remaining tasks to complete
pool.join();
assert_eq!(counter.load(sync::Ordering::Relaxed), 1000);
Run this example
Fields
-
inner: &mut ThreadPoolInner
Methods
impl ThreadPool { ... }
-
fn unbounded(num_threads: usize) -> ThreadPool
Creates a new thread pool with an unbounded task queue.
-
fn bounded(num_threads: usize, max_in_flight: usize) -> ThreadPool
Creates a new thread pool with a bounded task queue.
max_in_flight
is the maximum length of the task queue, if the queue is full, calling spawn will block. -
fn spawn<T, F>(self: &ThreadPool, f: F) -> Result<TaskHandle<T>, ChannelError>
F: Fn() -> TSpawns a new task on the thread pool, returning a handle that can be used to wait for the result.
-
fn spawn_detached<F>(self: &ThreadPool, f: F) -> Result<(), ChannelError>
F: Fn() -
fn close(self: &ThreadPool)
Completes the task queue, preventing new tasks to be spawned.
-
fn join(self: &ThreadPool)
Closes the thread pool and waits for all remaining tasks to finish.