Struct tinypool::ThreadPool

source ·
pub struct ThreadPool { /* private fields */ }
Expand description

A thread pool that holds a number of threads and executes jobs on them.

Implementations§

source§

impl ThreadPool

source

pub fn new(size: usize) -> Result<Self, ThreadPoolError>

Create a new thread pool with the given size.

Arguments
  • size - The number of threads to create in the thread pool. Use 0 to determine the number of threads automatically.
Returns

A Result containing the ThreadPool if successful, or a ThreadPoolError if unsuccessful.

Errors

A ThreadPoolError::ThreadSpawn will be returned if the thread pool failed to spawn a thread.

Examples
use tinypool::ThreadPool;

let threadpool = ThreadPool::new(4).unwrap();  // Create a thread pool with 4 threads
let threadpool2 = ThreadPool::new(0).unwrap();  // Create a thread pool with the number of threads determined automatically
source

pub fn execute<F>(&self, job: F) -> Result<(), ThreadPoolError>where F: FnOnce() + Send + 'static,

Add a job to the thread pool. Jobs are executed in the order they are added.

Arguments
  • job - The job (closure) to execute on a worker thread.
Returns

A Result containing () if successful, or a ThreadPoolError if unsuccessful.

Errors

A ThreadPoolError::EmptyPool will be returned if the thread pool is empty.

Examples
use tinypool::ThreadPool;
use std::sync::{Arc, Mutex};

let mut  threadpool = ThreadPool::new(4).unwrap();
let counter = Arc::new(Mutex::new(0));

for _ in 0..100 {
    let counter_thrd = Arc::clone(&counter);
    threadpool.execute(move || {
        let mut counter = counter_thrd.lock().unwrap();
        *counter += 1;
    }).unwrap();
}

threadpool.join();
assert_eq!(*counter.lock().unwrap(), 100);
source

pub fn queued(&self) -> usize

Get the number of queued (and running) jobs.

Returns

The number of queued jobs.

Examples
use tinypool::ThreadPool;
use std::thread;
use std::time::Duration;

let mut threadpool = ThreadPool::new(4).unwrap();

for _ in 0..8 {
    threadpool.execute(|| { thread::sleep(Duration::from_secs(1)); }).unwrap();
}

assert_eq!(threadpool.queued(), 8);  // 8 jobs (shouldn't have finished yet)
threadpool.join();
assert_eq!(threadpool.queued(), 0);  // 0 jobs (should have finished)
source

pub fn size(&self) -> usize

Get the number of threads in the thread pool.

Returns

The number of worker threads.

Examples
use tinypool::ThreadPool;

let threadpool = ThreadPool::new(4).unwrap();
assert_eq!(threadpool.size(), 4);

let threadpool2 = ThreadPool::new(0).unwrap();
assert_ne!(threadpool2.size(), 0);
source

pub fn set_size(&mut self, size: usize) -> Result<(), ThreadPoolError>

Set the number of threads in the thread pool. If you want to close all threads, use ThreadPool::join() instead. If reducing the number of threads, this method will close threads only after more jobs aren’t available.

Arguments
  • size - The number of threads to set the thread pool to. Use 0 to determine the number of threads automatically.
Returns

A Result containing () if successful, or a ThreadPoolError if unsuccessful.

Errors

A ThreadPoolError::ThreadSpawn will be returned if the thread pool failed to spawn a thread.

Examples
use tinypool::ThreadPool;
use std::thread;
use std::time::Duration;

let mut threadpool = ThreadPool::new(4).unwrap();
assert_eq!(threadpool.size(), 4);

for _ in 0..8 {
   threadpool.execute(|| { thread::sleep(Duration::from_secs(1)); }).unwrap();
}
assert_eq!(threadpool.queued(), 8);

threadpool.set_size(8).unwrap();
assert_eq!(threadpool.size(), 8);
assert_eq!(threadpool.queued(), 8);  // increasing thread pool size doesn't block main thread

threadpool.set_size(2).unwrap();
assert_eq!(threadpool.size(), 2);
assert!(threadpool.queued() <= 2);  // decreasing thread pool size blocks main thread until all jobs are finished

threadpool.join();
source

pub fn join(&mut self)

Wait for all queued jobs to finish and close all threads. This method will block until all queued jobs have finished. It will then close all threads in the thread pool. If you want to wait for all queued jobs to finish, but want to keep the threads running, use ThreadPool::wait() instead.

Examples
use tinypool::ThreadPool;
use std::thread;
use std::time::Duration;

let mut threadpool = ThreadPool::new(4).unwrap();
assert_eq!(threadpool.size(), 4);
assert_eq!(threadpool.queued(), 0);
for _ in 0..8 {
   threadpool.execute(|| { thread::sleep(Duration::from_secs(1)); }).unwrap();
}
threadpool.join();
assert_eq!(threadpool.size(), 0);
assert_eq!(threadpool.queued(), 0);
source

pub fn wait(&self)

Wait for all queued jobs to finish. This method will block until all queued jobs have finished. If you want to wait for all queued jobs to finish and close all threads, use ThreadPool::join() instead.

Examples
use tinypool::ThreadPool;
use std::thread;
use std::time::Duration;

let mut threadpool = ThreadPool::new(4).unwrap();
assert_eq!(threadpool.size(), 4);
assert_eq!(threadpool.queued(), 0);
for _ in 0..8 {
  threadpool.execute(|| { thread::sleep(Duration::from_secs(1)); }).unwrap();
}
threadpool.wait();
assert_eq!(threadpool.size(), 4);
assert_eq!(threadpool.queued(), 0);

Trait Implementations§

source§

impl Drop for ThreadPool

source§

fn drop(&mut self)

Drop the thread pool. This will wait for all queued jobs to finish and close all threads. Equivalent to ThreadPool::join().

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.