23 lines
No EOL
528 B
Rust
23 lines
No EOL
528 B
Rust
use std::sync::{Arc, Mutex};
|
|
use std::thread;
|
|
|
|
pub fn paralleler_zaehler(thread_anzahl: usize) -> i32 {
|
|
let zaehler = Arc::new(Mutex::new(0));
|
|
let mut handles = vec![];
|
|
|
|
for _ in 0..thread_anzahl {
|
|
let z = Arc::clone(&zaehler);
|
|
let handle = thread::spawn(move || {
|
|
let mut num = z.lock().unwrap();
|
|
*num += 1;
|
|
});
|
|
handles.push(handle);
|
|
}
|
|
|
|
for h in handles {
|
|
h.join().unwrap();
|
|
}
|
|
|
|
let final_val = *zaehler.lock().unwrap();
|
|
final_val
|
|
} |