<예제 코드_1>
use std::sync::{Mutex, Arc};
use std::thread;
fn main() {
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
let counter = Arc::clone(&counter);
let handle = thread::spawn(move || {
let mut num = counter.lock().unwrap();
*num += 1;
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("Result: {}", *counter.lock().unwrap());
}
<참조 1> https://rinthel.github.io/rust-lang-book-ko/ch16-03-shared-state.html
<참조 2>
'Rust' 카테고리의 다른 글
[Rust] Pattern : 여러 예제 (45일차) (0) | 2023.01.28 |
---|---|
[Rust] Trait 예제 (44일차) (0) | 2023.01.27 |
[Rust] Concurrency : Message Passing 예제 (42일차) (0) | 2023.01.25 |
[Rust] Concurrency : Thread 예제 (41일차) (0) | 2023.01.24 |
댓글