pub struct ThreadPool { /* private fields */ }Expand description
The thread pool of a lambda-channel that spawns threads running an infinite loop. The thread loop waits for a message from the input channel, executes a provided function, and sends the result to the output channel if the function has an output.
The pool starts with one control thread, all additional threads are normal worker threads. The control thread is identical to a normal worker thread, but also handles metrics collection, pool resizing, and termination propagation.
If the pool is dropped, the threads will automatically terminate. If the input channel or output channel to the thread pool disconnects, the threads will automatically terminate. If the thread is executing on an input or waiting to send an output when termination is triggered, it will finish execution and send the output value before terminating.
Implementations§
Source§impl ThreadPool
impl ThreadPool
Sourcepub fn get_pool_size(&self) -> usize
pub fn get_pool_size(&self) -> usize
Returns the target number of threads in the pool. The actual number of threads may temporarily differ when resizing the pool.
Sourcepub fn set_pool_size(&self, n: usize) -> Result<usize, ThreadPoolError>
pub fn set_pool_size(&self, n: usize) -> Result<usize, ThreadPoolError>
Sets the target number of threads in the pool. The actual number of threads may temporarily differ when resizing the pool.
This function will return the desired thread size if successful, or a ThreadPoolError if not.
Examples found in repository?
41fn main() {
42 let clock = quanta::Clock::new();
43 let start = clock.now();
44
45 let mut map: HashMap<char, AtomicU64> = HashMap::new();
46 let mut all_alphanumeric: Vec<char> = Vec::new();
47 all_alphanumeric.extend('0'..='9');
48 all_alphanumeric.extend('a'..='z');
49 all_alphanumeric.extend('A'..='Z');
50 for char in all_alphanumeric {
51 map.insert(char, AtomicU64::new(0));
52 }
53 let char_counts = Arc::new(map);
54
55 let (tx, rx, thread_pool) = new_lambda_channel(None, None, char_counts.clone(), process_file);
56 thread_pool.set_pool_size(4).unwrap();
57
58 let files = vec![
59 "./a.txt", "./b.txt", "./c.txt", "./d.txt", "./e.txt", "./f.txt",
60 ];
61
62 thread::spawn(move || {
63 for file in files {
64 tx.send(file).unwrap();
65 }
66 });
67
68 while let Ok(msg) = rx.recv() {
69 if let Err(e) = msg {
70 println!("Failed to open file: {}", e);
71 }
72 }
73
74 let mut total_counts: HashMap<char, u64> = HashMap::new();
75 for (k, v) in char_counts.iter() {
76 total_counts.insert(*k, v.load(Ordering::Relaxed));
77 }
78
79 println!("Execution Time: {:?}", start.elapsed());
80 println!("{:?}", total_counts);
81}Sourcepub fn get_metrics(&self) -> Metrics
pub fn get_metrics(&self) -> Metrics
Returns the latest metric values, which is updated approx. every 10 seconds. Calling this function between updates, will return the same values.
Trait Implementations§
Source§impl Clone for ThreadPool
impl Clone for ThreadPool
Source§fn clone(&self) -> ThreadPool
fn clone(&self) -> ThreadPool
1.0.0§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto 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§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)