Compare commits
No commits in common. "main" and "step-15" have entirely different histories.
2 changed files with 14 additions and 59 deletions
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
40
src/main.rs
40
src/main.rs
|
|
@ -1,33 +1,26 @@
|
|||
use log::info;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::sync::{Arc, Mutex, mpsc};
|
||||
use std::{sync, thread};
|
||||
use std::thread;
|
||||
|
||||
static GET: &'static [u8] = b"GET / HTTP/1.1\r\n";
|
||||
|
||||
pub struct ThreadPool {
|
||||
workers: Vec<Worker>,
|
||||
}
|
||||
|
||||
struct Worker{
|
||||
id: usize,
|
||||
thread: thread::JoinHandle<()>,
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
});
|
||||
fn new(id: usize) -> Worker {
|
||||
let thread = thread::spawn(|| {});
|
||||
Worker { id, thread }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ThreadPool {
|
||||
workers: Vec<Worker>,
|
||||
sender: mpsc::Sender<Job>,
|
||||
}
|
||||
|
||||
impl ThreadPool {
|
||||
/// Create a new ThreadPool
|
||||
///
|
||||
|
|
@ -37,24 +30,13 @@ impl ThreadPool {
|
|||
pub fn new(size: usize) -> Self {
|
||||
assert!(size > 0);
|
||||
let mut workers = Vec::with_capacity(size);
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
let receiver = Arc::new(Mutex::new(receiver));
|
||||
for id in 0..size {
|
||||
workers.push(Worker::new(id, Arc::clone(&receiver)))
|
||||
workers.push(Worker::new(id))
|
||||
}
|
||||
Self { workers, sender }
|
||||
Self {workers}
|
||||
}
|
||||
|
||||
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>;
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
env_logger::init();
|
||||
|
|
@ -62,7 +44,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(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue