reformatted crate `multithreading` to pass tests

This commit is contained in:
Sven Vogel 2023-05-30 22:25:08 +02:00
parent 1208a04658
commit 990a54a032
1 changed files with 20 additions and 11 deletions

View File

@ -36,7 +36,7 @@ use std::{
collections::VecDeque, collections::VecDeque,
num::NonZeroUsize, num::NonZeroUsize,
sync::{ sync::{
atomic::{AtomicUsize, Ordering, AtomicBool}, atomic::{AtomicBool, AtomicUsize, Ordering},
Arc, Mutex, Arc, Mutex,
}, },
thread::{self, JoinHandle}, thread::{self, JoinHandle},
@ -132,7 +132,7 @@ where
// will be initialized to 0 // will be initialized to 0
threads: Arc::new(AtomicUsize::new(0)), threads: Arc::new(AtomicUsize::new(0)),
// do not drop handles by default // do not drop handles by default
drop_handles: Arc::new(AtomicBool::new(false)) drop_handles: Arc::new(AtomicBool::new(false)),
} }
} }
} }
@ -176,7 +176,10 @@ where
/// if `drop_handles` is set to [`Bool::false`] the pool will continue to store the handles of /// if `drop_handles` is set to [`Bool::false`] the pool will continue to store the handles of
/// launched threads. This causes memory consumption to rise over time as more and more /// launched threads. This causes memory consumption to rise over time as more and more
/// threads are launched. /// threads are launched.
pub fn with_threads_and_drop_handles(max_thread_count: NonZeroUsize, drop_handles: bool) -> Self { pub fn with_threads_and_drop_handles(
max_thread_count: NonZeroUsize,
drop_handles: bool,
) -> Self {
Self { Self {
max_thread_count: max_thread_count.get(), max_thread_count: max_thread_count.get(),
drop_handles: Arc::new(AtomicBool::new(drop_handles)), drop_handles: Arc::new(AtomicBool::new(drop_handles)),
@ -318,7 +321,13 @@ fn execute<F, T>(
let next = queue.lock().unwrap().pop_front(); let next = queue.lock().unwrap().pop_front();
if let Some(next_closure) = next { if let Some(next_closure) = next {
// if we have sth. to execute, spawn a new thread // if we have sth. to execute, spawn a new thread
execute(queue, handles_copy.clone(), threads, drop.clone(), next_closure); execute(
queue,
handles_copy.clone(),
threads,
drop.clone(),
next_closure,
);
} else { } else {
// nothing to execute this thread will run out without any work to do // nothing to execute this thread will run out without any work to do
// decrement the amount of used threads // decrement the amount of used threads