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
impl ThreadPool
Sourcepub fn new(size: usize) -> Self
pub fn new(size: usize) -> Self
Create a new ThreadPool.
Sourcepub fn enqueue(&mut self, worker: impl Worker)
pub fn enqueue(&mut self, worker: impl Worker)
Enqueue a Worker to the ThreadPool.
Sourcepub fn update_threads(&mut self) -> bool
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.
Sourcepub const fn capacity(&self) -> usize
pub const fn capacity(&self) -> usize
Gets the capacity of the ThreadPool.
Sourcepub fn queued_count(&self) -> usize
pub fn queued_count(&self) -> usize
Returns the amount of enqueued Workers.
Sourcepub const fn active_count(&self) -> usize
pub const fn active_count(&self) -> usize
Returns the amount of running threads.
Sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Returns true when both the queue and pool are empty.
Auto Trait Implementations§
impl !RefUnwindSafe for ThreadPool
impl !Sync for ThreadPool
impl !UnwindSafe for ThreadPool
impl Freeze for ThreadPool
impl Send for ThreadPool
impl Unpin for ThreadPool
impl UnsafeUnpin for ThreadPool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more