Skip to content

Commit

Permalink
created Worker struct and updated ThreadPoll to store vec of Worker i…
Browse files Browse the repository at this point in the history
…nstead of thread
  • Loading branch information
dhanushrajgp committed Jul 13, 2024
1 parent 9ea77d7 commit 8072e4d
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::thread;

pub struct ThreadPool {
threads: Vec<thread::JoinHandle<()>>,
workers: Vec<Worker>,
}

impl ThreadPool {
Expand All @@ -15,11 +15,13 @@ impl ThreadPool {
pub fn new(size: usize) -> ThreadPool {
assert!(size > 0);

let mut threads = Vec::with_capacity(size);
let mut workers = Vec::with_capacity(size);

for _ in 0..size {}
for id in 0..size {
workers.push(Worker::new(id))
}

ThreadPool { threads }
ThreadPool { workers }
}

pub fn execute<F>(&self, f: F)
Expand All @@ -28,3 +30,15 @@ impl ThreadPool {
{
}
}

struct Worker {
id: usize,
thread: thread::JoinHandle<()>,
}

impl Worker {
pub fn new(id: usize) -> Worker {
let thread = thread::spawn(|| {});
Worker { id, thread }
}
}

0 comments on commit 8072e4d

Please sign in to comment.