Updated 'src/final_project/mutlithreaded.md'.

This commit is contained in:
Hector PENG
2026-04-28 16:37:00 +08:00
parent c759565d52
commit 1c0881cf05
4 changed files with 133 additions and 87 deletions

45
projects/hello/src/lib.rs Normal file
View 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 }
}
}

View File

@@ -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);