X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=simple-http%2Fsrc%2Flib.rs;fp=simple-http%2Fsrc%2Flib.rs;h=ff8bc4f3f8f63faecf47626d0f3a1ecff1082902;hp=0000000000000000000000000000000000000000;hb=4d4ba2d5f703969851742798e79e16333fb9ca9a;hpb=30342e681c6db184ebca5aa2c30ee8bb7f60470d diff --git a/simple-http/src/lib.rs b/simple-http/src/lib.rs new file mode 100644 index 0000000..ff8bc4f --- /dev/null +++ b/simple-http/src/lib.rs @@ -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, +} + +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(&self, f: F) + where F: FnOnce() + Send + 'static + { + thread::spawn(f); + } +}