2023-10-04 08:30:33 +00:00
|
|
|
use std::thread;
|
2023-10-04 08:55:44 +00:00
|
|
|
use std::sync::{mpsc,Arc};
|
|
|
|
use parking_lot::{Mutex,Condvar};
|
2023-10-04 08:57:18 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2023-10-04 08:30:33 +00:00
|
|
|
|
|
|
|
struct Worker {
|
|
|
|
id: usize,
|
2023-10-04 08:55:44 +00:00
|
|
|
receiver: Arc<(Mutex<mpsc::Receiver<Task>>, Condvar)>,
|
2023-10-04 08:57:18 +00:00
|
|
|
is_active: Arc<AtomicBool>,
|
2023-10-04 08:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Worker {
|
2023-10-04 08:57:18 +00:00
|
|
|
fn new(id: usize, receiver: Arc<(Mutex<mpsc::Receiver<Task>>, Condvar)>, is_active: Arc<AtomicBool>) -> Worker {
|
2023-10-04 08:37:01 +00:00
|
|
|
Worker { id, receiver, is_active }
|
2023-10-04 08:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn start(self) {
|
|
|
|
thread::spawn(move || {
|
|
|
|
loop {
|
2023-10-04 08:55:44 +00:00
|
|
|
let (ref lock, ref cvar) = &*self.receiver;
|
|
|
|
let task = lock.lock().unwrap().recv();
|
2023-10-04 08:30:33 +00:00
|
|
|
match task {
|
|
|
|
Ok(task) => {
|
|
|
|
println!("Worker {} got a task: {}", self.id, task);
|
|
|
|
// Process the task
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
println!("Worker {} stopping.", self.id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-04 08:37:01 +00:00
|
|
|
|
|
|
|
// Set is_active to false when the worker is done
|
2023-10-04 08:57:18 +00:00
|
|
|
self.is_active.store(false, Ordering::SeqCst);
|
|
|
|
self.receiver.1.notify_one();
|
2023-10-04 08:30:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Task = String;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let (sender, receiver) = mpsc::channel::<Task>();
|
2023-10-04 08:55:44 +00:00
|
|
|
let receiver = Arc::new((Mutex::new(receiver), Condvar::new()));
|
2023-10-04 08:57:18 +00:00
|
|
|
let is_active = Arc::new(AtomicBool::new(true));
|
2023-10-04 08:30:33 +00:00
|
|
|
|
2023-10-04 08:57:18 +00:00
|
|
|
// Create the worker thread
|
2023-10-04 08:37:01 +00:00
|
|
|
let worker = Worker::new(1, Arc::clone(&receiver), Arc::clone(&is_active));
|
2023-10-04 08:30:33 +00:00
|
|
|
|
2023-10-04 08:57:18 +00:00
|
|
|
// Start the worker thread
|
2023-10-04 08:30:33 +00:00
|
|
|
worker.start();
|
|
|
|
|
2023-10-04 08:57:18 +00:00
|
|
|
// Send tasks to the worker
|
2023-10-04 08:30:33 +00:00
|
|
|
for i in 0..5 {
|
|
|
|
let task = format!("Task {}", i);
|
|
|
|
sender.send(task).unwrap();
|
|
|
|
}
|
|
|
|
|
2023-10-04 08:57:18 +00:00
|
|
|
// Optional: Signal the worker to stop (in a real-world scenario)
|
2023-10-04 08:30:33 +00:00
|
|
|
// sender.send("STOP".to_string()).unwrap();
|
|
|
|
|
2023-10-04 08:57:18 +00:00
|
|
|
// Sleep to allow the worker thread to finish processing
|
2023-10-04 08:30:33 +00:00
|
|
|
thread::sleep(std::time::Duration::from_secs(2));
|
2023-10-04 08:34:24 +00:00
|
|
|
|
2023-10-04 08:57:18 +00:00
|
|
|
// Check if the worker is still active
|
|
|
|
let is_worker_active = is_active.load(Ordering::SeqCst);
|
2023-10-04 08:34:24 +00:00
|
|
|
|
2023-10-04 08:57:18 +00:00
|
|
|
if !is_worker_active {
|
|
|
|
// If the worker is done, signal it to process a new task
|
|
|
|
is_active.store(true, Ordering::SeqCst);
|
|
|
|
let (ref lock, ref cvar) = &*receiver;
|
|
|
|
cvar.notify_one();
|
|
|
|
|
|
|
|
// Send a new task
|
2023-10-04 08:37:01 +00:00
|
|
|
sender.send("New Task".to_string()).unwrap();
|
|
|
|
|
2023-10-04 08:57:18 +00:00
|
|
|
// Wait for the worker to finish processing
|
|
|
|
cvar.wait_while(lock.lock(), |&active| active);
|
2023-10-04 08:37:01 +00:00
|
|
|
} else {
|
2023-10-04 08:57:18 +00:00
|
|
|
println!("Worker is still active. Skipping new task.");
|
2023-10-04 08:37:01 +00:00
|
|
|
}
|
2023-10-04 08:34:24 +00:00
|
|
|
}
|