Mutex
Mutex<T>๋ ์ํธ๋ฐฐ์ ๋ฅผ ๋ณด์ฅํ๊ณ , ์ผ๊ธฐ์ ์ฉ ์ธํฐํ์ด์ค ๋ค์์ T์ ๋ํ ๊ฐ๋ณ ์ ๊ทผ์ ํ์ฉํฉ๋๋ค:
Mutex<T>ensures mutual exclusion and allows mutable access toTbehind a read-only interface:
use std::sync::Mutex; fn main() { let v: Mutex<Vec<i32>> = Mutex::new(vec![10, 20, 30]); println!("v: {:?}", v.lock().unwrap()); { let v: &Mutex<Vec<i32>> = &v; let mut guard = v.lock().unwrap(); guard.push(40); } println!("v: {:?}", v.lock().unwrap()); }
impl<T: Send> Sync for Mutex<T>๋ฅผ ์ฐธ์กฐํ์๊ธฐ ๋ฐ๋๋๋ค
Notice how we have a
impl<T: Send> Sync for Mutex<T>blanket implementation.