blob: d764879e5d02a11e28b8af03542c45d01c6cd52d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
use std::{sync::mpsc, thread};
pub struct ThreadPool {
workers: Vec<Worker>,
sender: mpsc::Sender<Job>,
}
struct Job;
// ANCHOR: here
impl ThreadPool {
// --snip--
// ANCHOR_END: here
/// Create a new ThreadPool.
///
/// The size is the number of threads in the pool.
///
/// # Panics
///
/// The `new` function will panic if the size is zero.
// ANCHOR: here
pub fn new(size: usize) -> ThreadPool {
assert!(size > 0);
let (sender, receiver) = mpsc::channel();
let mut workers = Vec::with_capacity(size);
for id in 0..size {
workers.push(Worker::new(id, receiver));
}
ThreadPool { workers, sender }
}
// --snip--
// ANCHOR_END: here
pub fn execute<F>(&self, f: F)
where
F: FnOnce() + Send + 'static,
{
}
// ANCHOR: here
}
// --snip--
// ANCHOR_END: here
struct Worker {
id: usize,
thread: thread::JoinHandle<()>,
}
// ANCHOR: here
impl Worker {
fn new(id: usize, receiver: mpsc::Receiver<Job>) -> Worker {
let thread = thread::spawn(|| {
receiver;
});
Worker { id, thread }
}
}
// ANCHOR_END: here
|