gpt: muh parking_lot

This commit is contained in:
Quaternions 2023-10-04 01:55:44 -07:00
parent 4933543dbf
commit 194b9b9a11
2 changed files with 13 additions and 9 deletions

View File

@ -11,6 +11,7 @@ mod framework;
mod primitives; mod primitives;
mod instruction; mod instruction;
mod load_roblox; mod load_roblox;
mod worker;
struct Entity { struct Entity {
index_count: u32, index_count: u32,

View File

@ -1,21 +1,23 @@
use std::thread; use std::thread;
use std::sync::{mpsc, Arc, Mutex}; use std::sync::{mpsc,Arc};
use parking_lot::{Mutex,Condvar};
struct Worker { struct Worker {
id: usize, id: usize,
receiver: Arc<Mutex<mpsc::Receiver<Task>>>, receiver: Arc<(Mutex<mpsc::Receiver<Task>>, Condvar)>,
is_active: Arc<Mutex<bool>>, is_active: Arc<Mutex<bool>>,
} }
impl Worker { impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Task>>>, is_active: Arc<Mutex<bool>>) -> Worker { fn new(id: usize, receiver: Arc<(Mutex<mpsc::Receiver<Task>>, Condvar)>, is_active: Arc<Mutex<bool>>) -> Worker {
Worker { id, receiver, is_active } Worker { id, receiver, is_active }
} }
fn start(self) { fn start(self) {
thread::spawn(move || { thread::spawn(move || {
loop { loop {
let task = self.receiver.lock().unwrap().recv(); let (ref lock, ref cvar) = &*self.receiver;
let task = lock.lock().unwrap().recv();
match task { match task {
Ok(task) => { Ok(task) => {
println!("Worker {} got a task: {}", self.id, task); println!("Worker {} got a task: {}", self.id, task);
@ -29,7 +31,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().unwrap() = false; *self.is_active.lock() = false;
self.receiver.1.notify_all();
}); });
} }
} }
@ -38,7 +41,7 @@ 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), Condvar::new()));
let is_active = Arc::new(Mutex::new(true)); let is_active = Arc::new(Mutex::new(true));
// Create the first worker thread // Create the first worker thread
@ -60,7 +63,7 @@ fn main() {
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 first worker is still active
let is_first_worker_active = *is_active.lock().unwrap(); let is_first_worker_active = *is_active.lock();
if !is_first_worker_active { if !is_first_worker_active {
// If the first worker is done, spawn a new worker // If the first worker is done, spawn a new worker
@ -68,8 +71,8 @@ fn main() {
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 // Wait for the new worker to finish processing
thread::sleep(std::time::Duration::from_secs(2)); let _ = receiver.1.wait_while(is_active.lock(), |&active| active);
} else { } else {
println!("First worker is still active. Skipping new worker."); println!("First worker is still active. Skipping new worker.");
} }