| 45 | } |
| 46 | |
| 47 | fn std_deadlock_wait() { |
| 48 | use std::sync::{Condvar, Mutex}; |
| 49 | |
| 50 | struct CondPair { |
| 51 | lock: Mutex<bool>, |
| 52 | cvar: Condvar, |
| 53 | other: Mutex<i32>, |
| 54 | } |
| 55 | |
| 56 | impl CondPair { |
| 57 | fn new() -> Self { |
| 58 | Self { |
| 59 | lock: Mutex::new(false), |
| 60 | cvar: Condvar::new(), |
| 61 | other: Mutex::new(1), |
| 62 | } |
| 63 | } |
| 64 | fn wait(&self) { |
| 65 | let _i = self.other.lock().unwrap(); |
| 66 | let mut started = self.lock.lock().unwrap(); |
| 67 | println!("start waiting!"); |
| 68 | while !*started { |
| 69 | started = self.cvar.wait(started).unwrap(); |
| 70 | } |
| 71 | println!("end waiting"); |
| 72 | } |
| 73 | fn notify(&self) { |
| 74 | let _i = self.other.lock().unwrap(); |
| 75 | let mut started = self.lock.lock().unwrap(); |
| 76 | println!("start notifying!"); |
| 77 | *started = true; |
| 78 | self.cvar.notify_one(); |
| 79 | println!("end notifying!"); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | let condvar1 = Arc::new(CondPair::new()); |
| 84 | let condvar2 = condvar1.clone(); |
| 85 | |
| 86 | let th1 = thread::spawn(move || { |
| 87 | condvar1.wait(); |
| 88 | }); |
| 89 | |
| 90 | condvar2.notify(); |
| 91 | th1.join().unwrap(); |
| 92 | } |
| 93 | |
| 94 | fn std_missing_lock_before_notify() { |
| 95 | use std::sync::{Condvar, Mutex}; |