29 lines
832 B
Rust
29 lines
832 B
Rust
// ignore-windows: Concurrency on Windows is not supported yet.
|
|
|
|
//! Check if Rust conditional variables are working.
|
|
|
|
use std::sync::{Arc, Condvar, Mutex};
|
|
use std::thread;
|
|
|
|
/// The test taken from the Rust documentation.
|
|
fn main() {
|
|
let pair = Arc::new((Mutex::new(false), Condvar::new()));
|
|
let pair2 = pair.clone();
|
|
|
|
// Inside of our lock, spawn a new thread, and then wait for it to start.
|
|
thread::spawn(move || {
|
|
let (lock, cvar) = &*pair2;
|
|
let mut started = lock.lock().unwrap();
|
|
*started = true;
|
|
// We notify the condvar that the value has changed.
|
|
cvar.notify_one();
|
|
});
|
|
|
|
// Wait for the thread to start up.
|
|
let (lock, cvar) = &*pair;
|
|
let mut started = lock.lock().unwrap();
|
|
while !*started {
|
|
started = cvar.wait(started).unwrap();
|
|
}
|
|
}
|