pub struct ThreadPool { /* private fields */ }
Expand description
A thread pool.
This can be cheaply cloned to create more handles to the same thread pool, so there is no need
to wrap it in an Arc
or similar type.
When dropped, the destructor won’t block but the thread pool itself will continue to run and
process tasks. You can call ThreadPool::wait_all_complete
to wait for all the active tasks
to complete.
Implementations§
Source§impl ThreadPool
impl ThreadPool
Sourcepub fn new() -> Self
pub fn new() -> Self
Construct a new thread pool using the default configuration. See Builder
for how to
customize it further.
Sourcepub unsafe fn spawn_raw<T>(&self, data: *const T, run: unsafe fn(*const T))
pub unsafe fn spawn_raw<T>(&self, data: *const T, run: unsafe fn(*const T))
Raw API for spawning a function on the thread pool.
This will call the given function pointer with the provided data
pointer on the blocking
thread pool at some point in the future.
§Safety
When called with the provided data pointer from another thread, the run
function must not
cause UB or panic.
Sourcepub fn spawn_boxed<F: FnOnce() + Send + 'static>(&self, f: Box<F>)
pub fn spawn_boxed<F: FnOnce() + Send + 'static>(&self, f: Box<F>)
Spawn a boxed closure on the thread pool.
§Examples
let pool = blocking_pool::ThreadPool::new();
pool.spawn_boxed(Box::new(|| println!("Hello world")));
Sourcepub fn wait_all_complete(&self)
pub fn wait_all_complete(&self)
Wait for all the running and queued tasks in the thread pool to complete.
§Examples
use std::time::{Duration, Instant};
let pool = blocking_pool::ThreadPool::new();
pool.spawn_boxed(Box::new(|| std::thread::sleep(Duration::from_secs(3))));
let start = Instant::now();
pool.wait_all_complete();
assert!(start.elapsed().as_secs() >= 3);
Sourcepub fn prune(&self)
pub fn prune(&self)
Prune unused threads. These threads would time out and exit eventually of their own accord if they are not receiving any work, but this will force them to exit early.
§Examples
let pool = blocking_pool::ThreadPool::new();
// This will start up a thread that will linger around even after the task is finished.
pool.spawn_boxed(Box::new(|| {}));
// This will forcibly kill that thread.
pool.prune();
Trait Implementations§
Source§impl Clone for ThreadPool
impl Clone for ThreadPool
Source§fn clone(&self) -> ThreadPool
fn clone(&self) -> ThreadPool
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more