Remove optional join handle from worker
This change removes the optional join handle from a worker, such that
the worker is consumed when it is joined.
@@ -119,16 +119,14 @@ impl ThreadPool {
}
pub fn join_all(mut self) {
- for worker in &mut self.workers {
+ for worker in self.workers.drain(..) {
debug!("Shutting down worker {}", worker.id);
- if let Some(thread) = worker.thread.take() {
- if let Err(_err) = thread.join() {
+ if let Err(_err) = worker.thread.join() {
warn!("Failed to cleanly join worker thread {}", worker.id);
}
}
}
}
-}
pub struct ShutdownSignaler {
worker_count: usize,
@@ -165,7 +163,7 @@ impl JobExecutor {
struct Worker {
id: usize,
- thread: Option<thread::JoinHandle<()>>,
+ thread: thread::JoinHandle<()>,
}
impl Worker {
@@ -174,8 +172,7 @@ impl Worker {
id: usize,
receiver: Arc<Mutex<mpsc::Receiver<Message>>>,
) -> Result<Worker, ThreadPoolBuildError> {
- let thread = Some(
- thread::Builder::new()
+ let thread = thread::Builder::new()
.name(format!("{}-{}", prefix, id))
.spawn(move || loop {
let msg = {
@@ -209,8 +206,7 @@ impl Worker {
})
.map_err(|err| {
ThreadPoolBuildError(format!("Unable to spawn worker thread: {}", err))
- })?,
- );
+ })?;
Ok(Worker { id, thread })
}
