Struct threadpool::ThreadPool [] [src]

pub struct ThreadPool {
    // some fields omitted
}

Abstraction of a thread pool for basic parallelism.

Methods

impl ThreadPool

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

Executes the function job on a thread in the pool.

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));
    });
}
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;
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 || {
        panic!()
    });
}
sleep(Duration::from_secs(1));
assert_eq!(pool.panic_count(), num_threads);

fn set_threads(&mut self, num_threads: usize)

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.

Trait Implementations

impl Debug for ThreadPool

fn fmt(&self, f: &mut Formatter) -> Result

Derived Implementations

impl Clone for ThreadPool

fn clone(&self) -> ThreadPool

1.0.0fn clone_from(&mut self, source: &Self)