From 5ca31a2b049c4ab5bbe9980a35d7526d22db62c8 Mon Sep 17 00:00:00 2001 From: Mitchell Date: Fri, 10 Apr 2020 10:57:47 +0000 Subject: [PATCH] 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 --- src/lib.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/lib.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..11ea4b2 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,52 @@ +use std::thread; + +pub struct ThreadPool{ + workers: Vec, +} + +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(&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, + } + } +}