Added a File to show how Threads are created in Rust

main
3008505 2026-06-13 12:23:22 +00:00
parent d9da49d8b9
commit 270ad82d4b
1 changed files with 25 additions and 0 deletions

View File

@ -0,0 +1,25 @@
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();
}