forked from StrafesNET/strafe-client
gpt: conditionally spawn new worker
This commit is contained in:
parent
f3f624c44c
commit
285a19a73a
@ -4,11 +4,12 @@ use std::sync::{mpsc, Arc, Mutex};
|
|||||||
struct Worker {
|
struct Worker {
|
||||||
id: usize,
|
id: usize,
|
||||||
receiver: Arc<Mutex<mpsc::Receiver<Task>>>,
|
receiver: Arc<Mutex<mpsc::Receiver<Task>>>,
|
||||||
|
is_active: Arc<Mutex<bool>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Worker {
|
impl Worker {
|
||||||
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Task>>>) -> Worker {
|
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Task>>>, is_active: Arc<Mutex<bool>>) -> Worker {
|
||||||
Worker { id, receiver }
|
Worker { id, receiver, is_active }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start(self) {
|
fn start(self) {
|
||||||
@ -26,6 +27,9 @@ impl Worker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set is_active to false when the worker is done
|
||||||
|
*self.is_active.lock().unwrap() = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -35,30 +39,38 @@ type Task = String;
|
|||||||
fn main() {
|
fn main() {
|
||||||
let (sender, receiver) = mpsc::channel::<Task>();
|
let (sender, receiver) = mpsc::channel::<Task>();
|
||||||
let receiver = Arc::new(Mutex::new(receiver));
|
let receiver = Arc::new(Mutex::new(receiver));
|
||||||
|
let is_active = Arc::new(Mutex::new(true));
|
||||||
|
|
||||||
// Create a worker thread
|
// Create the first worker thread
|
||||||
let worker = Worker::new(1, Arc::clone(&receiver));
|
let worker = Worker::new(1, Arc::clone(&receiver), Arc::clone(&is_active));
|
||||||
|
|
||||||
// Start the worker thread
|
// Start the first worker thread
|
||||||
worker.start();
|
worker.start();
|
||||||
|
|
||||||
// Send tasks to the worker
|
// Send tasks to the first worker
|
||||||
for i in 0..5 {
|
for i in 0..5 {
|
||||||
let task = format!("Task {}", i);
|
let task = format!("Task {}", i);
|
||||||
sender.send(task).unwrap();
|
sender.send(task).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optional: Signal the worker to stop (in a real-world scenario)
|
// Optional: Signal the first worker to stop (in a real-world scenario)
|
||||||
// sender.send("STOP".to_string()).unwrap();
|
// sender.send("STOP".to_string()).unwrap();
|
||||||
|
|
||||||
// Sleep to allow worker thread to finish processing
|
// Sleep to allow the first worker thread to finish processing
|
||||||
thread::sleep(std::time::Duration::from_secs(2));
|
thread::sleep(std::time::Duration::from_secs(2));
|
||||||
|
|
||||||
// Send another task, which will spawn a new worker
|
// Check if the first worker is still active
|
||||||
let new_worker = Worker::new(2, Arc::clone(&receiver));
|
let is_first_worker_active = *is_active.lock().unwrap();
|
||||||
|
|
||||||
|
if !is_first_worker_active {
|
||||||
|
// If the first worker is done, spawn a new worker
|
||||||
|
let new_worker = Worker::new(2, Arc::clone(&receiver), Arc::clone(&is_active));
|
||||||
new_worker.start();
|
new_worker.start();
|
||||||
sender.send("New Task".to_string()).unwrap();
|
sender.send("New Task".to_string()).unwrap();
|
||||||
|
|
||||||
// Sleep to allow the new worker thread to process the task
|
// Sleep to allow the new worker thread to process the task
|
||||||
thread::sleep(std::time::Duration::from_secs(2));
|
thread::sleep(std::time::Duration::from_secs(2));
|
||||||
|
} else {
|
||||||
|
println!("First worker is still active. Skipping new worker.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user