gpt: park thread

This commit is contained in:
Quaternions 2023-10-04 01:57:18 -07:00
parent 194b9b9a11
commit 8000d1db29

View File

@ -1,15 +1,16 @@
use std::thread; use std::thread;
use std::sync::{mpsc,Arc}; use std::sync::{mpsc,Arc};
use parking_lot::{Mutex,Condvar}; use parking_lot::{Mutex,Condvar};
use std::sync::atomic::{AtomicBool, Ordering};
struct Worker { struct Worker {
id: usize, id: usize,
receiver: Arc<(Mutex<mpsc::Receiver<Task>>, Condvar)>, receiver: Arc<(Mutex<mpsc::Receiver<Task>>, Condvar)>,
is_active: Arc<Mutex<bool>>, is_active: Arc<AtomicBool>,
} }
impl Worker { impl Worker {
fn new(id: usize, receiver: Arc<(Mutex<mpsc::Receiver<Task>>, Condvar)>, is_active: Arc<Mutex<bool>>) -> Worker { fn new(id: usize, receiver: Arc<(Mutex<mpsc::Receiver<Task>>, Condvar)>, is_active: Arc<AtomicBool>) -> Worker {
Worker { id, receiver, is_active } Worker { id, receiver, is_active }
} }
@ -31,8 +32,8 @@ impl Worker {
} }
// Set is_active to false when the worker is done // Set is_active to false when the worker is done
*self.is_active.lock() = false; self.is_active.store(false, Ordering::SeqCst);
self.receiver.1.notify_all(); self.receiver.1.notify_one();
}); });
} }
} }
@ -42,38 +43,41 @@ 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), Condvar::new())); let receiver = Arc::new((Mutex::new(receiver), Condvar::new()));
let is_active = Arc::new(Mutex::new(true)); let is_active = Arc::new(AtomicBool::new(true));
// Create the first worker thread // Create the worker thread
let worker = Worker::new(1, Arc::clone(&receiver), Arc::clone(&is_active)); let worker = Worker::new(1, Arc::clone(&receiver), Arc::clone(&is_active));
// Start the first worker thread // Start the worker thread
worker.start(); worker.start();
// Send tasks to the first worker // Send tasks to the 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 first worker to stop (in a real-world scenario) // Optional: Signal the worker to stop (in a real-world scenario)
// sender.send("STOP".to_string()).unwrap(); // sender.send("STOP".to_string()).unwrap();
// Sleep to allow the first worker thread to finish processing // Sleep to allow the worker thread to finish processing
thread::sleep(std::time::Duration::from_secs(2)); thread::sleep(std::time::Duration::from_secs(2));
// Check if the first worker is still active // Check if the worker is still active
let is_first_worker_active = *is_active.lock(); let is_worker_active = is_active.load(Ordering::SeqCst);
if !is_first_worker_active { if !is_worker_active {
// If the first worker is done, spawn a new worker // If the worker is done, signal it to process a new task
let new_worker = Worker::new(2, Arc::clone(&receiver), Arc::clone(&is_active)); is_active.store(true, Ordering::SeqCst);
new_worker.start(); let (ref lock, ref cvar) = &*receiver;
cvar.notify_one();
// Send a new task
sender.send("New Task".to_string()).unwrap(); sender.send("New Task".to_string()).unwrap();
// Wait for the new worker to finish processing // Wait for the worker to finish processing
let _ = receiver.1.wait_while(is_active.lock(), |&active| active); cvar.wait_while(lock.lock(), |&active| active);
} else { } else {
println!("First worker is still active. Skipping new worker."); println!("Worker is still active. Skipping new task.");
} }
} }