Here is the version that compiles:
use std::sync::{Arc, Mutex, Condvar};
use std::thread;
fn main() {
let thread_count_arc = Arc::new((Mutex::new(0u8), Condvar::new()));
let mut i = 0;
while i < 100 {
let thread_count = thread_count_arc.clone();
thread::spawn(move || {
let &(ref num, ref cvar) = &*thread_count;
{
let mut start = num.lock().unwrap();
while *start >= 20 {
start = cvar.wait(start).unwrap()
}
*start += 1;
}
println!("hello");
cvar.notify_one();
});
i += 1;
}
}
The important part can be seen from the waitAPI :
fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>>
, wait guard, , - start, !
, condvars - ( start), , .
, , wait. if while, dbaupp.