holy fuck i implemented a threadpool
Cargo Check, Format, Fix and Test / cargo CI (push) Successful in 2m10s
Details
Cargo Check, Format, Fix and Test / cargo CI (push) Successful in 2m10s
Details
This commit is contained in:
parent
25baf7cd2f
commit
a10bdc9015
|
@ -1,12 +1,17 @@
|
|||
use std::{
|
||||
io::{BufRead, BufReader, Write},
|
||||
io::{prelude::*, BufReader},
|
||||
net::{TcpListener, TcpStream},
|
||||
process::exit,
|
||||
};
|
||||
|
||||
mod pool;
|
||||
use pool::ThreadPool;
|
||||
|
||||
|
||||
fn main() {
|
||||
println!("Starting the server");
|
||||
let target_address = "127.0.0.1:7878";
|
||||
let pool = ThreadPool::build(4).unwrap();
|
||||
let listener = match TcpListener::bind(target_address) {
|
||||
Ok(listener) => listener,
|
||||
Err(err) => {
|
||||
|
@ -17,7 +22,9 @@ fn main() {
|
|||
for stream in listener.incoming() {
|
||||
let stream = stream.unwrap();
|
||||
|
||||
pool.execute(|| {
|
||||
handle_connection(stream);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
use std::{
|
||||
sync::{mpsc, Arc, Mutex},
|
||||
thread,
|
||||
};
|
||||
|
||||
struct WorkerThread {
|
||||
handle: thread::JoinHandle<()>,
|
||||
id: usize,
|
||||
}
|
||||
|
||||
impl WorkerThread {
|
||||
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> WorkerThread {
|
||||
let handle = thread::spawn(move || loop {
|
||||
let job = receiver.lock().unwrap().recv().unwrap();
|
||||
|
||||
println!("Worker {id} got a job; executing.");
|
||||
|
||||
job();
|
||||
});
|
||||
WorkerThread { handle, id }
|
||||
}
|
||||
}
|
||||
|
||||
/// Shares tasks over multiple worker threads
|
||||
pub struct ThreadPool {
|
||||
/// executes tasks assigned by the instructor
|
||||
workers: Vec<WorkerThread>,
|
||||
/// sends instructions to the workers
|
||||
sender: mpsc::Sender<Job>,
|
||||
}
|
||||
|
||||
/// Something for the workers to do
|
||||
type Job = Box<dyn FnOnce() + Send + 'static>;
|
||||
|
||||
impl ThreadPool {
|
||||
/// Create a new ThreadPool.
|
||||
///
|
||||
/// The size is the number of threads in the pool.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// The `new` function will panic if the size is zero.
|
||||
pub fn build(size: usize) -> Result<ThreadPool, String> {
|
||||
if !(size > 0) {
|
||||
return Err(format!("cannot build a thread pool with size 0!"));
|
||||
}
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
let receiver = Arc::new(Mutex::new(receiver));
|
||||
let mut workers = Vec::with_capacity(size);
|
||||
|
||||
for id in 0..size {
|
||||
// create some threads and store them in the vector
|
||||
workers.push(WorkerThread::new(id, Arc::clone(&receiver)));
|
||||
}
|
||||
|
||||
Ok(ThreadPool { workers, sender })
|
||||
}
|
||||
|
||||
/// schedules the given job for execution
|
||||
pub fn execute<F>(&self, f: F)
|
||||
where
|
||||
F: FnOnce() + Send + 'static,
|
||||
{
|
||||
// now it's getting crazy
|
||||
let job = Box::new(f);
|
||||
self.sender.send(job).unwrap();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue