问题描述
我试图使用lazy_static
声明和读写自定义结构的实例,因为我必须在其初始化(字符串)时使用非常量函数。
正如我在其他Stackoverflow帖子中看到的here一样,我尝试使用RwLock,它在写时工作正常,但在读时却失败,并出现以下错误:
thread 'main' panicked at 'rwlock read lock would result in deadlock',/Users/adrien/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/std/src/sys/unix/rwlock.rs:47:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
pub struct Authentication {
access_token: String,refresh_token: String,expiration: u32
}
lazy_static! {
static ref LATEST_AUTH: RwLock<Authentication> = RwLock::new(Authentication {
access_token: "access".to_string(),refresh_token: "refresh".to_string(),expiration: 0
});
}
pub fn auth(){
let api_resp: ApiResponse = res.json().unwrap(); //From a reqwest res
let mut au = LATEST_AUTH.write().unwrap();
au.access_token = api_resp.access_token.clone();
println!("LATEST_AUTH:{}",LATEST_AUTH.read().unwrap()); //Fails
}
解决方法
RwLock
在整个锁保护范围内都被锁定,这是通过调用read()
或write()
获得的。
在您的情况下,写锁定保护au
在auth
函数的整个过程中都有效。这就是错误的意思:您已经将其锁定,然后尝试再次将其锁定在同一线程中,将使其永远处于阻塞状态。
写锁也可以用于读取,因此您可以通过重新使用它来解决此问题,而不必尝试再次锁定它:
pub fn auth(){
let api_resp: ApiResponse = res.json().unwrap();
let mut au = LATEST_AUTH.write().unwrap();
au.access_token = api_resp.access_token.clone();
println!("LATEST_AUTH:{}",au);
}
或者,您可以强制将锁尽快放下,以便可以将其锁定以供单独阅读:
pub fn auth(){
let api_resp: ApiResponse = res.json().unwrap();
let mut au = LATEST_AUTH.write().unwrap();
au.access_token = api_resp.access_token.clone();
std::mem::drop(au);
println!("LATEST_AUTH:{}",LATEST_AUTH.read().unwrap());
}
或者:
pub fn auth(){
let api_resp: ApiResponse = res.json().unwrap();
// a new scope
{
let mut au = LATEST_AUTH.write().unwrap();
au.access_token = api_resp.access_token.clone();
// au will be dropped here,when it goes out of scope
}
println!("LATEST_AUTH:{}",LATEST_AUTH.read().unwrap());
}