Remove ThreadPool
Having switched to iterators and Rayon this is no longer used.
This commit is contained in:
parent
de41712291
commit
b509a5842f
2 changed files with 0 additions and 88 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
mod finder;
|
mod finder;
|
||||||
mod matcher;
|
mod matcher;
|
||||||
mod sorter;
|
mod sorter;
|
||||||
mod thread_pool;
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
|
|
|
||||||
|
|
@ -1,87 +0,0 @@
|
||||||
use std::sync::mpsc;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::sync::Mutex;
|
|
||||||
use std::thread;
|
|
||||||
|
|
||||||
enum Message {
|
|
||||||
NewJob(Job),
|
|
||||||
Terminate,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ThreadPool {
|
|
||||||
jobs: mpsc::Sender<Message>,
|
|
||||||
threads: Vec<Worker>,
|
|
||||||
}
|
|
||||||
|
|
||||||
trait FnBox {
|
|
||||||
fn call_box(self: Box<Self>);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F: FnOnce()> FnBox for F {
|
|
||||||
fn call_box(self: Box<F>) {
|
|
||||||
(*self)()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Job = Box<dyn FnBox + Send + 'static>;
|
|
||||||
|
|
||||||
impl ThreadPool {
|
|
||||||
pub fn new(thread_count: usize) -> Self {
|
|
||||||
let (jobs, receiver) = mpsc::channel();
|
|
||||||
let receiver = Arc::new(Mutex::new(receiver));
|
|
||||||
|
|
||||||
let mut threads: Vec<Worker> = Vec::new();
|
|
||||||
for id in 1..thread_count {
|
|
||||||
threads.push(Worker::new(id, Arc::clone(&receiver)));
|
|
||||||
}
|
|
||||||
|
|
||||||
ThreadPool { jobs, threads }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn execute<F>(&self, f: F)
|
|
||||||
where
|
|
||||||
F: FnOnce() + Send + 'static,
|
|
||||||
{
|
|
||||||
let job = Box::new(f);
|
|
||||||
self.jobs.send(Message::NewJob(job)).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for ThreadPool {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
for _ in &mut self.threads {
|
|
||||||
self.jobs.send(Message::Terminate).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
for worker in &mut self.threads {
|
|
||||||
if let Some(thread) = worker.thread.take() {
|
|
||||||
thread.join().unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Worker {
|
|
||||||
_id: usize,
|
|
||||||
thread: Option<thread::JoinHandle<()>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Worker {
|
|
||||||
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>) -> Worker {
|
|
||||||
let thread = thread::spawn(move || loop {
|
|
||||||
let message = receiver.lock().unwrap().recv().unwrap();
|
|
||||||
|
|
||||||
match message {
|
|
||||||
Message::NewJob(job) => job.call_box(),
|
|
||||||
Message::Terminate => {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Worker {
|
|
||||||
_id: id,
|
|
||||||
thread: Some(thread),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue