#21 call threadpool
This commit is contained in:
parent
c817629db0
commit
f2ed92564c
1 changed files with 9 additions and 3 deletions
12
src/main.rs
12
src/main.rs
|
|
@ -44,6 +44,14 @@ impl ThreadPool {
|
||||||
}
|
}
|
||||||
Self { workers, sender }
|
Self { workers, sender }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn execute<F>(&self, f: F)
|
||||||
|
where
|
||||||
|
F: FnOnce() + Send + 'static,
|
||||||
|
{
|
||||||
|
let job = Box::new(f);
|
||||||
|
self.sender.send(job).unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Job = Box<dyn FnOnce() + Send + 'static>;
|
type Job = Box<dyn FnOnce() + Send + 'static>;
|
||||||
|
|
@ -54,9 +62,7 @@ fn main() -> std::io::Result<()> {
|
||||||
let pool = ThreadPool::new(4);
|
let pool = ThreadPool::new(4);
|
||||||
for stream in listener.incoming() {
|
for stream in listener.incoming() {
|
||||||
let stream = stream?;
|
let stream = stream?;
|
||||||
thread::spawn(|| {
|
pool.execute(|| handle_connection(stream).unwrap());
|
||||||
handle_connection(stream).unwrap();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue