Compare commits

..

No commits in common. "main" and "step-19" have entirely different histories.

2 changed files with 7 additions and 45 deletions

View file

@ -1,29 +0,0 @@
{
"name": "Rust Workshop",
"image": "mcr.microsoft.com/devcontainers/rust:1-bullseye",
"features": {
"ghcr.io/devcontainers/features/git:1": {}
},
"customizations": {
"vscode": {
"extensions": [
"rust-lang.rust-analyzer",
"vadimcn.vscode-lldb",
"tamasfe.even-better-toml",
"humao.rest-client"
],
"settings": {
"editor.formatOnSave": true,
"rust-analyzer.check.command": "clippy"
}
}
},
"postCreateCommand": "cargo build",
"forwardPorts": [7878],
"portsAttributes": {
"7878": {
"label": "HTTP Server",
"onAutoForward": "notify"
}
}
}

View file

@ -1,7 +1,7 @@
use log::info;
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::{Arc, Mutex, mpsc};
use std::sync::{mpsc, Arc, Mutex};
use std::{sync, thread};
static GET: &'static [u8] = b"GET / HTTP/1.1\r\n";
@ -13,11 +13,8 @@ struct Worker {
impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
let thread = thread::spawn(move || {
loop {
let job = receiver.lock().unwrap().recv().unwrap();
job();
}
let thread = thread::spawn(|| {
receiver;
});
Worker { id, thread }
}
@ -44,17 +41,9 @@ impl ThreadPool {
}
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>;
struct Job {}
fn main() -> std::io::Result<()> {
env_logger::init();
@ -62,7 +51,9 @@ fn main() -> std::io::Result<()> {
let pool = ThreadPool::new(4);
for stream in listener.incoming() {
let stream = stream?;
pool.execute(|| handle_connection(stream).unwrap());
thread::spawn(|| {
handle_connection(stream).unwrap();
});
}
Ok(())
}