]>
Commit | Line | Data |
---|---|---|
74b04a01 | 1 | // ANCHOR: here |
923072b8 | 2 | use std::{sync::mpsc, thread}; |
74b04a01 XL |
3 | |
4 | pub struct ThreadPool { | |
5 | workers: Vec<Worker>, | |
6 | sender: mpsc::Sender<Job>, | |
7 | } | |
8 | ||
9 | struct Job; | |
10 | ||
11 | impl 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 | ||
47 | struct Worker { | |
48 | id: usize, | |
49 | thread: thread::JoinHandle<()>, | |
50 | } | |
51 | ||
52 | impl Worker { | |
53 | fn new(id: usize) -> Worker { | |
54 | let thread = thread::spawn(|| {}); | |
55 | ||
56 | Worker { id, thread } | |
57 | } | |
58 | } |