Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Simple thread

  • In this case when the main thread does not do "extra job" it is obvious that the other thread did not even have a chance to start working.
use std::thread;

fn main() {
    println!("Before starting: {:?}", thread::current().id());

    thread::spawn(|| {
        println!("In thread {:?}", thread::current().id());
    });

    println!("After ending: {:?}", thread::current().id());
}
Before starting: ThreadId(1)
After ending: ThreadId(1)

  • thread
  • spawn