]> git.proxmox.com Git - rustc.git/blame - src/doc/book/listings/ch20-web-server/listing-20-16/src/lib.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / doc / book / listings / ch20-web-server / listing-20-16 / src / lib.rs
CommitLineData
74b04a01 1// ANCHOR: here
923072b8 2use std::{sync::mpsc, thread};
74b04a01
XL
3
4pub struct ThreadPool {
5 workers: Vec<Worker>,
6 sender: mpsc::Sender<Job>,
7}
8
9struct Job;
10
11impl ThreadPool {
12 // --snip--
13 // ANCHOR_END: here
14 /// Create a new ThreadPool.
15 ///
16 /// The size is the number of threads in the pool.
17 ///
18 /// # Panics
19 ///
20 /// The `new` function will panic if the size is zero.
21 // ANCHOR: here
22 pub fn new(size: usize) -> ThreadPool {
23 assert!(size > 0);
24
25 let (sender, receiver) = mpsc::channel();
26
27 let mut workers = Vec::with_capacity(size);
28
29 for id in 0..size {
30 workers.push(Worker::new(id));
31 }
32
33 ThreadPool { workers, sender }
34 }
35 // --snip--
36 // ANCHOR_END: here
37
38 pub fn execute<F>(&self, f: F)
39 where
40 F: FnOnce() + Send + 'static,
41 {
42 }
43 // ANCHOR: here
44}
45// ANCHOR_END: here
46
47struct Worker {
48 id: usize,
49 thread: thread::JoinHandle<()>,
50}
51
52impl Worker {
53 fn new(id: usize) -> Worker {
54 let thread = thread::spawn(|| {});
55
56 Worker { id, thread }
57 }
58}