contains code for workers and thread pool
librarys for creating a thread pool of 'n' size and specifying a worker for each thread in the thread pool
This commit is contained in:
parent
73614e4fa0
commit
5ca31a2b04
|
|
@ -0,0 +1,52 @@
|
||||||
|
use std::thread;
|
||||||
|
|
||||||
|
pub struct ThreadPool{
|
||||||
|
workers: Vec<Worker>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ThreadPool{
|
||||||
|
/// Create a new ThreadPool.
|
||||||
|
///
|
||||||
|
/// The size is the number of threads in the pool.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// The `new` function will panic if the size is zero.
|
||||||
|
pub fn new(size: usize) -> ThreadPool {
|
||||||
|
assert!(size > 0);
|
||||||
|
|
||||||
|
let mut workers = Vec::with_capacity(size);
|
||||||
|
|
||||||
|
for id in 0..size {
|
||||||
|
// create some threads and store them in the vector
|
||||||
|
workers.push(Worker::new(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
ThreadPool {
|
||||||
|
workers
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn execute<F>(&self, f: F)
|
||||||
|
where
|
||||||
|
F: FnOnce() + Send + 'static
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Worker {
|
||||||
|
id: usize,
|
||||||
|
thread: thread::JoinHandle<()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Worker {
|
||||||
|
fn new(id: usize) -> Worker {
|
||||||
|
let thread = thread::spawn(|| {});
|
||||||
|
|
||||||
|
Worker {
|
||||||
|
id,
|
||||||
|
thread,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue