PR3-Rust-SS26/G-concurrency/threads.rs

25 lines
605 B
Rust

use std::thread;
fn main() {
// 1. Spawn the first thread
let handle1 = thread::spawn(|| {
let mut my_number = 0; // Starts at 0
for _ in 0..10 {
my_number += 1;
println!("Thread 1, Number {}", my_number);
}
});
// 2. Spawn the second thread
let handle2 = thread::spawn(|| {
let mut my_number = 0;
for _ in 0..10 {
my_number += 1;
println!("Thread 2, Number {}", my_number);
}
});
// 3. Wait for both threads to finish
handle1.join().unwrap();
handle2.join().unwrap();
}