Skip to content

Commit babedc9

Browse files
committed
Rewrite notify all test.
1 parent 8b5a983 commit babedc9

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

tests/run-pass/concurrency/sync.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,32 @@ fn check_conditional_variables_notify_one() {
5353
}
5454
}
5555

56-
/// The test taken from the Rust documentation.
5756
fn check_conditional_variables_notify_all() {
58-
let pair = Arc::new((Mutex::new(false), Condvar::new()));
59-
let pair2 = pair.clone();
57+
let pair = Arc::new(((Mutex::new(())), Condvar::new()));
6058

61-
thread::spawn(move || {
62-
let (lock, cvar) = &*pair2;
63-
let mut started = lock.lock().unwrap();
64-
*started = true;
65-
// We notify the condvar that the value has changed.
66-
cvar.notify_all();
67-
});
59+
// Spawn threads and block them on the conditional variable.
60+
let handles: Vec<_> = (0..5)
61+
.map(|_| {
62+
let pair2 = pair.clone();
63+
thread::spawn(move || {
64+
let (lock, cvar) = &*pair2;
65+
let guard = lock.lock().unwrap();
66+
// Block waiting on the conditional variable.
67+
let _ = cvar.wait(guard).unwrap();
68+
})
69+
})
70+
.inspect(|_| {
71+
thread::yield_now();
72+
thread::yield_now();
73+
})
74+
.collect();
6875

69-
// Wait for the thread to start up.
70-
let (lock, cvar) = &*pair;
71-
let mut started = lock.lock().unwrap();
72-
// As long as the value inside the `Mutex<bool>` is `false`, we wait.
73-
while !*started {
74-
started = cvar.wait(started).unwrap();
76+
let (_, cvar) = &*pair;
77+
// Unblock all threads.
78+
cvar.notify_all();
79+
80+
for handle in handles {
81+
handle.join().unwrap();
7582
}
7683
}
7784

0 commit comments

Comments
 (0)