Struct ThreadPool

Source
pub struct ThreadPool { /* private fields */ }
Expand description

ThreadPool provides a way to manage and execute tasks concurrently using a fixed number of worker threads.

It allows you to submit tasks that will be executed by one of the available worker threads, providing an efficient way to parallelize work across multiple threads.

Implementations§

Source§

impl ThreadPool

Source

pub fn builder() -> ThreadPoolBuilder

Discovery method for ThreadPoolBuilder.

Returns a default ThreadPoolBuilder for constructing a new ThreadPool.

This method provides a convenient way to start building a ThreadPool with default settings, which can then be customized as needed.

§Examples
use base_threadpool::ThreadPool;

let pool = ThreadPool::builder().build();
Source

pub fn execute<F>(&self, f: F)
where F: FnOnce() + Send + 'static,

Schedules a task to be executed by the thread pool.

Panics if the thread pool has been shut down or if there’s an error sending the job.

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

// Create a thread pool with 4 worker threads
let pool = ThreadPool::builder().num_threads(4).build();

// Create a list of items to process
let items = vec!["apple", "banana", "cherry", "date", "elderberry"];
let processed_items = Arc::new(Mutex::new(Vec::new()));

// Process each item concurrently
for item in items {
    let processed_items = Arc::clone(&processed_items);
    pool.execute(move || {
        // Simulate some processing time
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Process the item (in this case, convert to uppercase)
        let processed = item.to_uppercase();

        // Store the processed item
        processed_items.lock().unwrap().push(processed);
    });
}
Source

pub fn join(&mut self)

Waits for all worker threads in the pool to finish their current tasks and then shuts down the pool.

This function will block until all workers have completed.

§Examples
use base_threadpool::ThreadPool;
use std::sync::{
    atomic::{AtomicU16, Ordering},
    Arc,
};

let mut pool = ThreadPool::builder().build();
let counter = Arc::new(AtomicU16::new(0));

(0..100).for_each(|_| {
    let counter = Arc::clone(&counter);
    pool.execute(move || {
        let _ = counter.fetch_add(1, Ordering::SeqCst);
    });
});

pool.join();
assert_eq!(counter.load(Ordering::SeqCst), 100);
Source

pub fn num_threads(&self) -> usize

Provides information about the level of concurrency available in the thread pool.

Returns the number of worker threads in the pool.

§Examples
use base_threadpool::ThreadPool;

let pool = ThreadPool::builder().num_threads(4).build();
assert_eq!(pool.num_threads(), 4);

Trait Implementations§

Source§

impl Debug for ThreadPool

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for ThreadPool

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.