Compare commits

...

8 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
Shautvast
bbf889e72e #16 threadpool #5 2026-02-09 20:13:08 +01:00
Shautvast
24f0ca7de1 #15 threadpool #4 2026-02-09 20:09:57 +01:00
2 changed files with 68 additions and 11 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,12 +1,31 @@
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::thread; use std::sync::{Arc, Mutex, mpsc};
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";
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();
}
});
Worker { id, thread }
}
}
pub struct ThreadPool { pub struct ThreadPool {
threads: Vec<thread::JoinHandle<()>>, workers: Vec<Worker>,
sender: mpsc::Sender<Job>,
} }
impl ThreadPool { impl ThreadPool {
@ -15,26 +34,35 @@ impl ThreadPool {
/// # Panics /// # Panics
/// ///
/// The `new` function panics if the size is zero /// The `new` function panics if the size is zero
pub fn new(size: usize) -> Self { pub fn new(size: usize) -> Self {
assert!(size > 0); assert!(size > 0);
let threads = Vec::with_capacity(size); let mut workers = Vec::with_capacity(size);
for _ in 0..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)))
} }
Self {threads} 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>;
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
env_logger::init(); env_logger::init();
let listener = TcpListener::bind("127.0.0.1:7878")?; let listener = TcpListener::bind("127.0.0.1:7878")?;
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(())
} }