Park and unpark the thread

fn main() {
    use std::thread;
    use std::time::Duration;

    let parked_thread = thread::Builder::new()
        .spawn(|| {
            println!("Parking thread.");
            thread::park();
            println!("Thread unparked.")
        })
        .unwrap();

    thread::sleep(Duration::from_secs(10));

    println!("Unpark the thread");
    parked_thread.thread().unpark();

    parked_thread.join().unwrap();
}

(Playground)

Hi and welcome to the Rust users forum. It looks like you have found the option to share code from the rust Playground here. I see you marked your post as “tutorial”. If your goal is to teach someone something, maybe you should a few words though.

Or maybe we can use this thread to answer any questions you might still have about your code? Feel free to ask.

Realistically, you should always add a bit of English text to posts in this forum, otherwise it’s not really clear what kind of answers you are interested in.

As it stands, this is not really any good for a “tutorial”, so I will be removing that categorization.


Also, if you look up the documentation of thread::park you’ll find it comes with an example not unlike yours already.

If you study this example, you’ll notice that it is slightly more complex though; it makes sure to correctly account for the possibility of “spurious wakeups”. Feel free to read the documentation on that in more detail; and of course you can – for example – also ask questions here, if that’s still unclear afterwards.

And also feel free to come back to this forum with any other problem or question on Rust that you have and can't easily find the answer to. Meanwhile, have fun with the Rust programming language - cheers :slight_smile:

2 Likes

Okay, I see.