mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2026-08-19 12:43:28 +08:00
Updated 'src/final_project/mutlithreaded.md'.
This commit is contained in:
45
projects/hello/src/lib.rs
Normal file
45
projects/hello/src/lib.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use std::thread;
|
||||
|
||||
pub struct ThreadPool {
|
||||
workers: Vec<Worker>,
|
||||
}
|
||||
|
||||
impl ThreadPool {
|
||||
/// 创建一个新的 ThreadPool。
|
||||
///
|
||||
/// 其中 size 为线程池中线程的数量。
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// `new` 函数将在 size 为零时终止运行。
|
||||
pub fn new(size: usize) -> ThreadPool {
|
||||
assert! (size > 0);
|
||||
|
||||
let mut threads = Vec::with_capacity(size);
|
||||
|
||||
for _ in 0..size {
|
||||
workers.push(Worker::new(id));
|
||||
}
|
||||
|
||||
ThreadPool { workers }
|
||||
}
|
||||
|
||||
pub fn execute<F>(&self, f: F)
|
||||
where
|
||||
F: FnOnce() + Send + 'static,
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
struct Worker {
|
||||
id: usize,
|
||||
thread: thread::JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl Worker {
|
||||
fn new(id: usize) -> Worker {
|
||||
let thread = thread::spawn(|| {});
|
||||
|
||||
Worker { id, thread }
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ use std::{
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use hello::ThreadPool;
|
||||
|
||||
fn main() {
|
||||
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
|
||||
let pool = ThreadPool::new(4);
|
||||
|
||||
Reference in New Issue
Block a user