Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::sync::atomic::{AtomicBool, Ordering};
- use std::sync::Arc;
- use std::time::Duration;
- use std::thread;
- fn actually_run() -> thread::JoinHandle<()> {
- let running = Arc::new(AtomicBool::new(true));
- let running_for_thread = running.clone();
- // Spawn a thread that loops until running is `false`.
- let t = thread::spawn(move || {
- while running.load(Ordering::Relaxed) {
- thread::sleep(Duration::from_millis(1));
- }
- thread::sleep(Duration::from_secs(4));
- running.store(true, Ordering::Relaxed);
- println!("thread: exiting!");
- });
- // Do some work in the parent
- println!("parent: running");
- thread::sleep(Duration::from_secs(3));
- // Tell thread to stop, and join thread
- running_for_thread.store(false, Ordering::Relaxed);
- println!("parent: exiting!");
- t
- }
- fn main() {
- let tt = actually_run();
- tt.join().expect("NOPE")
- }
Add Comment
Please, Sign In to add comment