나는 Rust에서 멀티 쓰레딩에 대해 놀려고하고 있는데, 나는 사소한 것으로 생각할만한 것을 발견했다. 여기 코드는 다음과 같습니다왜 시도 할 수 없습니까! 뮤텍스. 록?
use std::thread;
use std::sync::{Arc, Mutex};
fn main() {
let vec: Vec<i32> = vec!(1, 2, 3);
let shared = Arc::new(Mutex::new(vec));
let clone = shared.clone();
let join_handle = thread::spawn(move || {
let mut data = clone.lock().unwrap();
data.push(5);
});
join_handle.join().unwrap();
let clone = shared.clone();
let vec = try!(clone.lock());
println!("{:?}", *vec);
}
그래서, 내 문제는 라인 let vec = try!(clone.lock())
에 있습니다. 이로 인해 다음과 같은 컴파일러 오류가 발생합니다.
error: mismatched types [E0308]
return $ crate :: result :: Result :: Err (
^
note: in this expansion of try! (defined in <std macros>)
help: run `rustc --explain E0308` to see a detailed explanation
note: expected type `()`
note: found type `std::result::Result<_, _>`
나에게 이것은별로 의미가 없습니다. clone.lock()
은 TryLockResult<MutexGuard<T>>
을 반환하며, 이는 기본적으로 Result<MutexGuard<T>, PoisonedError<MutexGuard<T>>
으로 변환됩니다. 이는 try!(clone.lock())
이 throw 또는 MutexGuard<T>
으로 해결되어야 함을 의미합니다.
여기에 근본적으로 오해가 있습니까?