Hi, I'm using OnceLock
, Arc
and Mutex
to create a global singleton with thread safety, and it works fine already:
// An example
fn get_instance() -> &'static Arc<Mutex<String>> {
static INSTANCE: OnceLock<Arc<Mutex<String>>> = OnceLock::new();
INSTANCE.get_or_init(|| {
Arc::new(Mutex::new(String::new()))
})
}
// In some other thread
let lock = get_instance().lock().unwrap();
As the rust document says, Arc
is an atomic reference counter for sharing ownerships, but in the example above it is already static, the lifetime is the whole program. I think here tracing the reference counter is unnecessary.
Can I replace OnceLock<Arc<Mutex<String>>>
to OnceLock<Mutex<String>>
so just make the get_instance
function returns a &'static Mutex<String>
?