#20 threadpool finished
This commit is contained in:
parent
35e2a66628
commit
c817629db0
1 changed files with 7 additions and 4 deletions
11
src/main.rs
11
src/main.rs
|
|
@ -1,7 +1,7 @@
|
||||||
use log::info;
|
use log::info;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::net::{TcpListener, TcpStream};
|
use std::net::{TcpListener, TcpStream};
|
||||||
use std::sync::{mpsc, Arc, Mutex};
|
use std::sync::{Arc, Mutex, mpsc};
|
||||||
use std::{sync, thread};
|
use std::{sync, thread};
|
||||||
|
|
||||||
static GET: &'static [u8] = b"GET / HTTP/1.1\r\n";
|
static GET: &'static [u8] = b"GET / HTTP/1.1\r\n";
|
||||||
|
|
@ -13,8 +13,11 @@ struct Worker {
|
||||||
|
|
||||||
impl Worker {
|
impl Worker {
|
||||||
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
|
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
|
||||||
let thread = thread::spawn(|| {
|
let thread = thread::spawn(move || {
|
||||||
receiver;
|
loop {
|
||||||
|
let job = receiver.lock().unwrap().recv().unwrap();
|
||||||
|
job();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
Worker { id, thread }
|
Worker { id, thread }
|
||||||
}
|
}
|
||||||
|
|
@ -43,7 +46,7 @@ impl ThreadPool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Job {}
|
type Job = Box<dyn FnOnce() + Send + 'static>;
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue