Compare commits

...

6 commits

Author SHA1 Message Date
Shautvast
f829f816ba codespaces devcontainer 2026-03-17 13:52:05 +01:00
Shautvast
f2ed92564c #21 call threadpool 2026-02-12 16:49:30 +01:00
Shautvast
c817629db0 #20 threadpool finished 2026-02-09 20:39:46 +01:00
Shautvast
35e2a66628 #19 threadpool #8 2026-02-09 20:26:55 +01:00
Shautvast
18923b0952 #18 threadpool #7 2026-02-09 20:26:07 +01:00
Shautvast
e4211e27bf #17 threadpool #6 2026-02-09 20:24:19 +01:00
2 changed files with 54 additions and 13 deletions

View file

@ -0,0 +1,29 @@
{
"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,28 +1,33 @@
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; 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";
pub struct ThreadPool {
workers: Vec<Worker>,
sender: mpsc::Sender<Job>,
}
struct Worker { struct Worker {
id: usize, id: usize,
thread: thread::JoinHandle<()>, thread: thread::JoinHandle<()>,
} }
impl Worker { impl Worker {
fn new(id: usize) -> Worker { fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
let thread = thread::spawn(|| {}); let thread = thread::spawn(move || {
loop {
let job = receiver.lock().unwrap().recv().unwrap();
job();
}
});
Worker { id, thread } Worker { id, thread }
} }
} }
pub struct ThreadPool {
workers: Vec<Worker>,
sender: mpsc::Sender<Job>,
}
impl ThreadPool { impl ThreadPool {
/// Create a new ThreadPool /// Create a new ThreadPool
/// ///
@ -33,14 +38,23 @@ impl ThreadPool {
assert!(size > 0); assert!(size > 0);
let mut workers = Vec::with_capacity(size); let mut workers = Vec::with_capacity(size);
let (sender, receiver) = mpsc::channel(); let (sender, receiver) = mpsc::channel();
let receiver = Arc::new(Mutex::new(receiver));
for id in 0..size { for id in 0..size {
workers.push(Worker::new(id)) workers.push(Worker::new(id, Arc::clone(&receiver)))
} }
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();
}
} }
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();
@ -48,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(())
} }