Struct threadpool::ThreadPool [] [src]

pub struct ThreadPool { /* fields omitted */ }

Abstraction of a thread pool for basic parallelism.

Methods

impl ThreadPool
[src]

Spawns a new thread pool with num_threads threads.

Panics

This function will panic if num_threads is 0.

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);
}

Executes the function job on a thread in the pool.

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);

Returns the number of created threads

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);

Deprecated since 1.3.0

: use ThreadPool::set_num_threads

Deprecated: Use ThreadPool::set_num_threads

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 Clone for ThreadPool
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for ThreadPool
[src]

Formats the value using the given formatter.