From 662b32e2633639a0b4a61f098e89a5e18d85d601 Mon Sep 17 00:00:00 2001 From: dhanushrajgp Date: Sat, 13 Jul 2024 12:31:15 +0530 Subject: [PATCH] Created Channels to communicate between threads --- src/lib.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b6528ab..1f90f15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,8 @@ -use std::thread; +use std::{sync::mpsc, thread}; pub struct ThreadPool { workers: Vec, + sender: mpsc::Sender, } impl ThreadPool { @@ -14,6 +15,7 @@ impl ThreadPool { /// The `new` function will panic if the size is zero. pub fn new(size: usize) -> ThreadPool { assert!(size > 0); + let (sender, reciever) = mpsc::channel(); let mut workers = Vec::with_capacity(size); @@ -21,7 +23,7 @@ impl ThreadPool { workers.push(Worker::new(id)) } - ThreadPool { workers } + ThreadPool { workers, sender } } pub fn execute(&self, f: F) @@ -31,6 +33,7 @@ impl ThreadPool { } } +struct Job; struct Worker { id: usize, thread: thread::JoinHandle<()>,