The first piece of code that the conclusions of the auxiliary (involving multi-threaded, the realization of a plurality of variable reference)
use std::sync::Arc;
use std::sync::Mutex;
struct Point {
pub x: u32,
pub y: u32
}
impl Point {
pub fn get_instance() -> Arc<Mutex<Point>> {
static mut POINT: Option<Arc<Mutex<Point>>> = None;
unsafe {// Rust中使用可变静态变量都是unsafe的
POINT.get_or_insert_with(|| {
// 初始化单例对象的代码
Arc::new(Mutex::new(Point {x: 0, y: 0}))
}).clone()
}
}
}
- Used
Option<...>
as a static variable to store the original global singleton object pointer, by get_or_insert_with method to initialize the singleton object - The most rigorous approach is to use
Arc<Mutex<T>>
orArc<RwLock<T>>
to hold the singleton object; if the variable does not need to reference the singleton object directlyArc<T>
can; if it is single-threaded program,Arc
can be usedRc
alternatively
Why not Box but Arc / Rc?
Box represents a unique pointer reference, can not clone. Singleton object is to be referenced by multiple codes Box<T>
can only move can not clone, it is impossible to achieve one Box<T>
as a shared pointer.
The situation does not require variable references
If no variable references, it is not necessary to use a Mutex or RwLock. Can return a Arc<T>
or&'static T
pub fn get_instance() -> Arc<Point> {
static mut POINT: Option<Arc<Point>> = None;
unsafe {// Rust中使用可变静态变量都是unsafe的
POINT.get_or_insert_with(|| {
// 初始化单例对象的代码
Arc::new(Point {x: 0, y: 0})
}).clone()
}
}
// 返回&'static Point
pub fn get_instance() -> &'staic Point {
static mut POINT: Option<Arc<Point>> = None;
unsafe {// Rust中使用可变静态变量都是unsafe的
POINT.get_or_insert_with(|| {
// 初始化单例对象的代码
Arc::new(Point {x: 0, y: 0})
});
POINT.as_ref().unwrap()
}
}