Struct threadpool::ThreadPool
[−]
[src]
pub struct ThreadPool { /* fields omitted */ }
Abstraction of a thread pool for basic parallelism.
Methods
impl ThreadPool
[src]
fn new(num_threads: usize) -> ThreadPool
Spawns a new thread pool with num_threads
threads.
Panics
This function will panic if num_threads
is 0.
fn new_with_name(name: String, num_threads: usize) -> ThreadPool
Spawns a new thread pool with num_threads
threads. Each thread will have the
name name
.
Panics
This function will panic if num_threads
is 0.
Examples
use std::sync::mpsc::sync_channel; use std::thread; use threadpool::ThreadPool; let (tx, rx) = sync_channel(0); let mut pool = ThreadPool::new_with_name("worker".into(), 2); for _ in 0..2 { let tx = tx.clone(); pool.execute(move || { let name = thread::current().name().unwrap().to_owned(); tx.send(name).unwrap(); }); } for thread_name in rx.iter().take(2) { assert_eq!("worker", thread_name); }
fn execute<F>(&self, job: F) where
F: FnOnce() + Send + 'static,
F: FnOnce() + Send + 'static,
Executes the function job
on a thread in the pool.
fn queued_count(&self) -> usize
Returns the number of accepted jobs
fn active_count(&self) -> usize
Returns the number of currently active threads.
Examples
use threadpool::ThreadPool; use std::time::Duration; use std::thread::sleep; let num_threads = 10; let pool = ThreadPool::new(num_threads); for _ in 0..num_threads { pool.execute(move || { sleep(Duration::from_secs(5)); }); } // wait for the pool to start working sleep(Duration::from_secs(1)); assert_eq!(pool.active_count(), num_threads);
fn max_count(&self) -> usize
Returns the number of created threads
fn panic_count(&self) -> usize
Returns the number of panicked threads over the lifetime of the pool.
Examples
use threadpool::ThreadPool; let num_threads = 10; let pool = ThreadPool::new(num_threads); for _ in 0..num_threads { pool.execute(move || { panic!() }); } pool.join(); assert_eq!(pool.panic_count(), num_threads);
fn set_threads(&mut self, num_threads: usize)
: use ThreadPool::set_num_threads
Deprecated: Use ThreadPool::set_num_threads
fn set_num_threads(&mut self, num_threads: usize)
Sets the number of worker-threads to use as num_threads
.
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_threads
is 0.
fn join(&self)
Block the current thread until all jobs in the pool are completed. Many threads can wait for a pool to finish concurrently.
use threadpool::ThreadPool; use std::time::Duration; use std::thread::sleep; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; let pool = ThreadPool::new_with_name("join test".to_string(), 8); let test_count = Arc::new(AtomicUsize::new(0)); for _ in 0..42 { let test_count = test_count.clone(); pool.execute(move || { // Some long running calculation sleep(Duration::from_secs(2)); test_count.fetch_add(1, Ordering::Relaxed); }); } pool.join(); assert_eq!(42, test_count.load(Ordering::Relaxed));
Trait Implementations
impl Clone for ThreadPool
[src]
fn clone(&self) -> ThreadPool
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more