From 270ad82d4be188d29a23281abc72999dbc0fb35b Mon Sep 17 00:00:00 2001 From: 3008505 <3008505.studs.hs-mannheim.de> Date: Sat, 13 Jun 2026 12:23:22 +0000 Subject: [PATCH] Added a File to show how Threads are created in Rust --- G-concurrency/threads.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 G-concurrency/threads.rs diff --git a/G-concurrency/threads.rs b/G-concurrency/threads.rs new file mode 100644 index 0000000..f62668d --- /dev/null +++ b/G-concurrency/threads.rs @@ -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(); +} \ No newline at end of file