trpl-zh-cn/listings/ch20-web-server/listing-20-16/src/lib.rs
2022-02-06 16:43:51 +08:00

61 lines
1.1 KiB
Rust
Executable File

use std::thread;
// ANCHOR: here
// --snip--
use std::sync::mpsc;
pub struct ThreadPool {
workers: Vec<Worker>,
sender: mpsc::Sender<Job>,
}
struct Job;
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));
}
ThreadPool { workers, sender }
}
// --snip--
// ANCHOR_END: here
pub fn execute<F>(&self, f: F)
where
F: FnOnce() + Send + 'static,
{
}
// ANCHOR: here
}
// ANCHOR_END: here
struct Worker {
id: usize,
thread: thread::JoinHandle<()>,
}
impl Worker {
fn new(id: usize) -> Worker {
let thread = thread::spawn(|| {});
Worker { id, thread }
}
}