9951 explained code solutions for 126 technologies


rustHow to dump a thread in Rust?


Dumping a thread in Rust is done using the thread::park() function. This function will block the current thread until it is unparked.

use std::thread;

let handle = thread::spawn(|| {
    println!("Hello from a thread!");
});

handle.thread().unpark();
thread::park();

The code above will spawn a new thread and then unpark it. The main thread will then be parked until the spawned thread is finished.

  • thread::spawn(): spawns a new thread
  • handle.thread().unpark(): unpark the spawned thread
  • thread::park(): park the main thread

Helpful links

Edit this code on GitHub