Function std::thread::park1.0.0[][src]

pub fn park()

Blocks unless or until the current thread's token is made available.

A call to park does not guarantee that the thread will remain parked forever, and callers should be prepared for this possibility.

park and unpark

Every thread is equipped with some basic low-level blocking support, via the thread::park function and thread::Thread::unpark method. park blocks the current thread, which can then be resumed from another thread by calling the unpark method on the blocked thread's handle.

Conceptually, each Thread handle has an associated token, which is initially not present:

In other words, each Thread acts a bit like a spinlock that can be locked and unlocked using park and unpark.

The API is typically used by acquiring a handle to the current thread, placing that handle in a shared data structure so that other threads can find it, and then parking. When some desired condition is met, another thread calls unpark on the handle.

The motivation for this design is twofold:

Examples

use std::thread;
use std::time::Duration;

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

// Let some time pass for the thread to be spawned.
thread::sleep(Duration::from_millis(10));

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

parked_thread.join().unwrap();Run