Struct poolio::ThreadPool
source · pub struct ThreadPool { /* private fields */ }
Expand description
Abstracts the thread-pools.
Implementations§
source§impl ThreadPool
impl ThreadPool
sourcepub fn new<'a>(size: usize, mode: PanicSwitch) -> Result<Self, &'a str>
pub fn new<'a>(size: usize, mode: PanicSwitch) -> Result<Self, &'a str>
Sets up a new pool.
size
is the (non-zero) number of worker-threads in the pool.mode
is the setting of the panic switch.
Errors
An error is returned if 0 was passed as size
(since a pool without worker-threads does not make sense).
Examples
Setting up a pool with three worker-threads in kill-mode:
let pool = poolio::ThreadPool::new(3, poolio::PanicSwitch::Kill).unwrap();
sourcepub fn execute<F>(&self, f: F)where
F: FnOnce() + UnwindSafe + Send + 'static,
pub fn execute<F>(&self, f: F)where F: FnOnce() + UnwindSafe + Send + 'static,
Runs a job in self
.
f
is the job to be run and has to be provided as a certain closure.
Note that if f
panics, the behavior is according to the setting of the PanicSwitch
of self
.
Panics
A panic is caused if the pool is unreachable.
Examples
Setting up a pool and printing two strings concurrently:
let pool = poolio::ThreadPool::new(2, poolio::PanicSwitch::Kill).unwrap();
pool.execute(|| println!{"house"});
pool.execute(|| println!{"cat"});
Trait Implementations§
source§impl Drop for ThreadPool
impl Drop for ThreadPool
source§fn drop(&mut self)
fn drop(&mut self)
Tries to shut down self
gracefully.
In particular, one has to assume that all remaining jobs will be finished (modulo panics in PanicSwitch::Kill
-mode).
Panics
A panic occurs if
- the pool is unreachable
- joining the threads panics.
Remember that a panic while dropping aborts the whole process.