Skip to main content

ThreadPool

Struct ThreadPool 

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

A basic Thread Pool

§Examples

use std::thread;
use std::time::Duration;
use xuko_core::thread_pool::ThreadPool;

let mut pool = ThreadPool::new(8);
for i in 0..pool.capacity() {
    let v = (i + 1) as u64;
    pool.enqueue(move || {
        thread::sleep(Duration::from_secs(v));
        println!("Task finished! {v}")
    });
}
// blocks until all threads in the pool finish
pool.run_all();

Implementations§

Source§

impl ThreadPool

Source

pub fn new(size: usize) -> Self

Create a new ThreadPool.

Source

pub fn enqueue(&mut self, worker: impl Worker)

Enqueue a Worker to the ThreadPool.

Source

pub fn update_threads(&mut self) -> bool

Removes threads from the pool once they’re finished and spawn new ones from the queue.

Returns true if the pool is empty even after emptying the queue.

Source

pub const fn capacity(&self) -> usize

Gets the capacity of the ThreadPool.

Source

pub fn queued_count(&self) -> usize

Returns the amount of enqueued Workers.

Source

pub const fn active_count(&self) -> usize

Returns the amount of running threads.

Source

pub fn is_finished(&self) -> bool

Returns true when both the queue and pool are empty.

Source

pub fn run_all(&mut self)

Blocks until all the active threads to finish and the queue is empty

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.