Guest User

Untitled

a guest
Jan 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. use std::sync::atomic::{AtomicBool, Ordering};
  2. use std::sync::Arc;
  3. use std::time::Duration;
  4. use std::thread;
  5.  
  6. fn actually_run() -> thread::JoinHandle<()> {
  7. let running = Arc::new(AtomicBool::new(true));
  8. let running_for_thread = running.clone();
  9.  
  10. // Spawn a thread that loops until running is `false`.
  11. let t = thread::spawn(move || {
  12. while running.load(Ordering::Relaxed) {
  13. thread::sleep(Duration::from_millis(1));
  14. }
  15. thread::sleep(Duration::from_secs(4));
  16. running.store(true, Ordering::Relaxed);
  17. println!("thread: exiting!");
  18. });
  19.  
  20. // Do some work in the parent
  21. println!("parent: running");
  22. thread::sleep(Duration::from_secs(3));
  23.  
  24. // Tell thread to stop, and join thread
  25. running_for_thread.store(false, Ordering::Relaxed);
  26. println!("parent: exiting!");
  27. t
  28. }
  29.  
  30. fn main() {
  31. let tt = actually_run();
  32. tt.join().expect("NOPE")
  33.  
  34. }
Add Comment
Please, Sign In to add comment