]> piware.de Git - learn-rust.git/blob - simple-http/src/lib.rs
ff8bc4f3f8f63faecf47626d0f3a1ecff1082902
[learn-rust.git] / simple-http / src / lib.rs
1 use std::thread;
2
3 struct Worker {
4     id: usize,
5     thread: thread::JoinHandle<()>,
6 }
7
8 impl Worker {
9     fn new(id: usize) -> Worker {
10         Worker { id, thread: thread::spawn(|| {}) }
11     }
12 }
13
14 pub struct ThreadPool {
15     workers: Vec<Worker>,
16 }
17
18 impl ThreadPool {
19     /// Create a new thread pool.
20     ///
21     /// # Panics
22     ///
23     /// - if size is zero
24     pub fn new(size: usize) -> ThreadPool {
25         assert!(size > 0);
26         let mut workers = Vec::with_capacity(size);
27
28         for id in 0..size {
29             workers.push(Worker::new(id));
30         }
31
32         ThreadPool { workers }
33     }
34
35     pub fn execute<F>(&self, f: F)
36     where F: FnOnce() + Send + 'static
37     {
38         thread::spawn(f);
39     }
40 }