pub struct ThreadPool { /* private fields */ }
Expand description
Thread pool of workers awaiting execution orders.
Implementations§
Source§impl ThreadPool
impl ThreadPool
Sourcepub fn new(capacity: usize) -> ThreadPool
pub fn new(capacity: usize) -> ThreadPool
Create a new ThreadPool.
The capacity is the number of desired threads in the pool.
§Panics
The new
function will panic if the capacity is zero.
§Examples
use std::sync::Arc;
use std::sync::atomic::{ Ordering, AtomicBool };
use threaded::ThreadPool;
let num_workers = 2;
let tp = ThreadPool::new(num_workers);
assert_eq!(tp.capacity(), num_workers);
let has_executed = Arc::new(AtomicBool::new(false));
{
let has_executed = has_executed.clone();
tp.execute(move || {
has_executed.swap(true, Ordering::SeqCst);
});
}
drop(tp); // block main thread until execute finishes (uses handle.join())
assert_eq!(has_executed.load(Ordering::SeqCst), true);
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ThreadPool
impl !RefUnwindSafe for ThreadPool
impl Send for ThreadPool
impl Sync for ThreadPool
impl Unpin for ThreadPool
impl !UnwindSafe 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