mirror of https://github.com/KaiserY/trpl-zh-cn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.2 KiB
66 lines
1.2 KiB
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
|