| 2 | use std::thread; |
| 3 | |
| 4 | fn std_correct() { |
| 5 | use std::sync::{Condvar, Mutex}; |
| 6 | |
| 7 | struct CondPair { |
| 8 | lock: Mutex<bool>, |
| 9 | cvar: Condvar, |
| 10 | } |
| 11 | |
| 12 | impl CondPair { |
| 13 | fn new() -> Self { |
| 14 | Self { |
| 15 | lock: Mutex::new(false), |
| 16 | cvar: Condvar::new(), |
| 17 | } |
| 18 | } |
| 19 | fn wait(&self) { |
| 20 | let mut started = self.lock.lock().unwrap(); |
| 21 | println!("start waiting!"); |
| 22 | while !*started { |
| 23 | started = self.cvar.wait(started).unwrap(); |
| 24 | } |
| 25 | println!("end waiting"); |
| 26 | } |
| 27 | fn notify(&self) { |
| 28 | let mut started = self.lock.lock().unwrap(); |
| 29 | println!("start notifying!"); |
| 30 | *started = true; |
| 31 | self.cvar.notify_one(); |
| 32 | println!("end notifying!"); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | let condvar1 = Arc::new(CondPair::new()); |
| 37 | let condvar2 = condvar1.clone(); |
| 38 | |
| 39 | let th1 = thread::spawn(move || { |
| 40 | condvar1.wait(); |
| 41 | }); |
| 42 | |
| 43 | condvar2.notify(); |
| 44 | th1.join().unwrap(); |
| 45 | } |
| 46 | |
| 47 | fn std_deadlock_wait() { |
| 48 | use std::sync::{Condvar, Mutex}; |