Knowledge Garden

Search

Search IconIcon to open search

Threads in Rust

Last updated Dec 1, 2022 Edit Source

# Threads

In rust threading is cross platform, mac win linux and more.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
use std::thread;

fn main() {
	let handle = thread::spawn(move || {
		// do stuff in a child thread
	});
	// do stuff simultaneously in the main thead
	
	// wait until thread has exited
	handle.join().unwrap();
}

thread spawn takes a closure with no arguments. spawn returns a join handle.

don’t use threads for waiting for stuff use async stuff instead