Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #![allow(unused)]
  2. fn main() {
  3. use std::sync::{Arc, Mutex, Condvar};
  4. use std::thread;
  5.  
  6. let pair = Arc::new((Mutex::new(false), Condvar::new()));
  7. let pair2 = pair.clone();
  8.  
  9. // Inside of our lock, spawn a new thread, and then wait for it to start.
  10. thread::spawn(move|| {
  11. {
  12. let &(ref lock, ref cvar) = &*pair2;
  13. let mut started = lock.lock().unwrap();
  14. *started = true;
  15. // We notify the condvar that the value has changed.
  16. cvar.notify_one();
  17.  
  18. println!("Notified");
  19. }
  20.  
  21. thread::sleep_ms(0xffffff);
  22. });
  23.  
  24. // Wait for the thread to start up.
  25. let &(ref lock, ref cvar) = &*pair;
  26. let mut started = lock.lock().unwrap();
  27. while !*started {
  28. started = cvar.wait(started).unwrap();
  29. }
  30.  
  31. println!("Started");
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement