Struct threadpool::ThreadPool [] [src]

pub struct ThreadPool {
    // some fields omitted
}

A thread pool used to execute functions in parallel.

Spawns n worker threads and replenishes the pool if any worker threads panic.

Examples

Syncronized with a channel

Every thread sends one message over the channel, which then is collected with the take().

use threadpool::ThreadPool;
use std::sync::mpsc::channel;

let n_workers = 4;
let n_jobs = 8;
let pool = ThreadPool::new(n_workers);

let (tx, rx) = channel();
for i in 0..n_jobs {
    let tx = tx.clone();
    pool.execute(move|| {
        tx.send(i).unwrap();
    });
}

assert_eq!(rx.iter().take(n_jobs).fold(0, |a, b| a + b), 28);

Syncronized with a barrier

Keep in mind, if you put more jobs in the pool than you have workers, you will end up with a deadlock which is not considered unsafe.

use threadpool::ThreadPool;
use std::sync::{Arc, Barrier};
use std::sync::atomic::{AtomicUsize, Ordering};

// create at least as many workers as jobs or you will deadlock yourself
let n_workers = 42;
let n_jobs = 23;
let pool = ThreadPool::new(n_workers);
let an_atomic = Arc::new(AtomicUsize::new(0));

// create a barrier that wait all jobs plus the starter thread
let barrier = Arc::new(Barrier::new(n_jobs + 1));
for i in 0..n_jobs {
  let barrier = barrier.clone();
  let an_atomic = an_atomic.clone();

  pool.execute(move|| {
    // do the heavy work
    an_atomic.fetch_add(1, Ordering::Relaxed);

    // then wait for the other threads
    barrier.wait();
  });
}

// wait for the threads to finish the work
barrier.wait();
assert_eq!(an_atomic.load(Ordering::SeqCst), 23);

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.

Example

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.

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.

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.

Trait Implementations

Derived Implementations

impl Clone for ThreadPool

fn clone(&self) -> ThreadPool

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