]> piware.de Git - learn-rust.git/blobdiff - simple-http/src/lib.rs
simple-http: Add scaffolding for thread pool implementation
[learn-rust.git] / simple-http / src / lib.rs
diff --git a/simple-http/src/lib.rs b/simple-http/src/lib.rs
new file mode 100644 (file)
index 0000000..ff8bc4f
--- /dev/null
@@ -0,0 +1,40 @@
+use std::thread;
+
+struct Worker {
+    id: usize,
+    thread: thread::JoinHandle<()>,
+}
+
+impl Worker {
+    fn new(id: usize) -> Worker {
+        Worker { id, thread: thread::spawn(|| {}) }
+    }
+}
+
+pub struct ThreadPool {
+    workers: Vec<Worker>,
+}
+
+impl ThreadPool {
+    /// Create a new thread pool.
+    ///
+    /// # Panics
+    ///
+    /// - if size is zero
+    pub fn new(size: usize) -> ThreadPool {
+        assert!(size > 0);
+        let mut workers = Vec::with_capacity(size);
+
+        for id in 0..size {
+            workers.push(Worker::new(id));
+        }
+
+        ThreadPool { workers }
+    }
+
+    pub fn execute<F>(&self, f: F)
+    where F: FnOnce() + Send + 'static
+    {
+        thread::spawn(f);
+    }
+}