January 2019
Beginner to intermediate
554 pages
13h 31m
English
As we said, every program starts with a main thread. To create an independent execution point from anywhere in the program, the main thread can spawn a new thread, which becomes its child thread. Child threads can further spawn their own threads. Let's look at a concurrent program in Rust that uses threads in the simplest way possible:
// thread_basics.rsuse std::thread;fn main() { thread::spawn(|| { println!("Thread!"); "Much concurrent, such wow!".to_string() }); print!("Hello ");}
In main, we call the spawn function from the thread module which takes a no parameter closure as an argument. Within this closure, we can write any code that we want to execute concurrently as a separate thread. In our closure, we simply print some ...
Read now
Unlock full access