pub struct ThreadPool { /* private fields */ }
Expand description
Abstraction of a thread pool for basic parallelism.
Implementations§
Source§impl ThreadPool
impl ThreadPool
Sourcepub fn execute<F>(&self, job: F)
pub fn execute<F>(&self, job: F)
Executes the function job
on a thread in the pool.
§Examples
Execute four jobs on a thread pool that can run two jobs concurrently:
let pool = lft_rust::lft_auto_config();
pool.execute(|| println!("hello"));
pool.execute(|| println!("world"));
pool.execute(|| println!("foo"));
pool.execute(|| println!("bar"));
pool.join();
Sourcepub fn queued_count(&self) -> usize
pub fn queued_count(&self) -> usize
Returns the number of jobs waiting to executed in the pool.
§Examples
use threadpool::ThreadPool;
use std::time::Duration;
use std::thread::sleep;
let pool = lft_rust::lft_builder()
.num_workers(2)
.build();
for _ in 0..10 {
pool.execute(|| {
sleep(Duration::from_secs(100));
});
}
sleep(Duration::from_secs(1)); // wait for threads to start
assert_eq!(8, pool.queued_count());
Sourcepub fn active_count(&self) -> usize
pub fn active_count(&self) -> usize
Returns the number of currently active worker threads.
§Examples
use std::time::Duration;
use std::thread::sleep;
let pool = lft_rust::lft_builder()
.num_workers(4)
.build();
for _ in 0..10 {
pool.execute(move || {
sleep(Duration::from_secs(100));
});
}
sleep(Duration::from_secs(1)); // wait for threads to start
assert_eq!(4, pool.active_count());
Sourcepub fn max_count(&self) -> usize
pub fn max_count(&self) -> usize
Returns the maximum number of threads the pool will execute concurrently.
§Examples
let pool = lft_rust::lft_builder()
.num_workers(4)
.build();
assert_eq!(4, pool.max_count());
pool.set_num_workers(8);
assert_eq!(8, pool.max_count());
Sourcepub fn num_workers(&self) -> usize
pub fn num_workers(&self) -> usize
Returns the number of workers running in the threadpool.
Sourcepub fn panic_count(&self) -> usize
pub fn panic_count(&self) -> usize
Returns the number of panicked threads over the lifetime of the pool.
§Examples
let pool = lft_rust::lft_builder()
.num_workers(4)
.build();
for n in 0..10 {
pool.execute(move || {
// simulate a panic
if n % 2 == 0 {
panic!()
}
});
}
pool.join();
assert_eq!(5, pool.panic_count());
Sourcepub fn spawn_extra_one_worker(&self)
pub fn spawn_extra_one_worker(&self)
Sets the number of worker-threads to use as num_workers
.
Can be used to change the threadpool size during runtime.
Will not abort already running or waiting threads.
§Panics
This function will panic if num_workers
is 0.
§Examples
use std::time::Duration;
use std::thread::sleep;
let pool = threadpool::builder().num_workers(4).build();
for _ in 0..10 {
pool.execute(move || {
sleep(Duration::from_secs(100));
});
}
sleep(Duration::from_secs(1)); // wait for threads to start
assert_eq!(4, pool.active_count());
assert_eq!(6, pool.queued_count());
// Increase thread capacity of the pool
pool.set_num_workers(8);
sleep(Duration::from_secs(1)); // wait for new threads to start
assert_eq!(8, pool.active_count());
assert_eq!(2, pool.queued_count());
// Decrease thread capacity of the pool
// No active threads are killed
pool.set_num_workers(4);
assert_eq!(8, pool.active_count());
assert_eq!(2, pool.queued_count());
Spawn an extra worker thread. Can be used to increase the number of work threads during runtimes. If the number of threads is already the maximum, it will print out an warning.
§Examples
use std::time::Duration;
use std::thread::sleep;
let pool = threadpool::builder().num_workers(4).build();
for _ in 0..10 {
pool.execute(move || {
sleep(Duration::from_secs(100));
});
pool.spawn_extra_one_worker();
Sourcepub fn shutdown_one_worker(&self)
pub fn shutdown_one_worker(&self)
Shutdown a worker thread in the threadpool. Can be used to increase the number of work threads during runtimes. If the number of threads is already 0, it will print out an warning.
§Examples
use std::time::Duration;
use std::thread::sleep;
let pool = threadpool::builder().num_workers(4).build();
for _ in 0..10 {
pool.execute(move || {
sleep(Duration::from_secs(100));
});
pool.spawn_extra_one_worker();
Sourcepub fn join(&self)
pub fn join(&self)
Block the current thread until all jobs in the pool have been executed.
Calling join
on an empty pool will cause an immediate return.
join
may be called from multiple threads concurrently.
A join
is an atomic point in time. All threads joining before the join
event will exit together even if the pool is processing new jobs by the
time they get scheduled.
Calling join
from a thread within the pool will cause a deadlock. This
behavior is considered safe.
Note: Join will not stop the worker threads. You will need to drop
all instances of ThreadPool
for the worker threads to terminate.
§Examples
use threadpool::ThreadPool;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
let pool = lft_rust::lft_auto_config();
let test_count = Arc::new(AtomicUsize::new(0));
for _ in 0..42 {
let test_count = test_count.clone();
pool.execute(move || {
test_count.fetch_add(1, Ordering::Relaxed);
});
}
pool.join();
assert_eq!(42, test_count.load(Ordering::Relaxed));
Trait Implementations§
Source§impl Clone for ThreadPool
impl Clone for ThreadPool
Source§fn clone(&self) -> ThreadPool
fn clone(&self) -> ThreadPool
Cloning a pool will create a new handle to the pool. The behavior is similar to Arc.
We could for example submit jobs from multiple threads concurrently.
use std::thread;
use crossbeam_channel::unbounded;
let pool = lft_rust::lft_builder()
.worker_name("clone example")
.num_workers(2)
.build();
let results = (0..2)
.map(|i| {
let pool = pool.clone();
thread::spawn(move || {
let (tx, rx) = unbounded();
for i in 1..12 {
let tx = tx.clone();
pool.execute(move || {
tx.send(i).expect("channel will be waiting");
});
}
drop(tx);
if i == 0 {
rx.iter().fold(0, |accumulator, element| accumulator + element)
} else {
rx.iter().fold(1, |accumulator, element| accumulator * element)
}
})
})
.map(|join_handle| join_handle.join().expect("collect results from threads"))
.collect::<Vec<usize>>();
assert_eq!(vec![66, 39916800], results);
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for ThreadPool
impl Debug for ThreadPool
Source§impl PartialEq for ThreadPool
impl PartialEq for ThreadPool
Source§fn eq(&self, other: &ThreadPool) -> bool
fn eq(&self, other: &ThreadPool) -> bool
Check if you are working with the same pool
let a = lft_rust::lft_auto_config();
let b = lft_rust::lft_auto_config();
assert_eq!(a, a);
assert_eq!(b, b);
assert_ne!(a, b);
assert_ne!(b, a);