In my current project there is a lot inter dependency between different structs, so instead of having Arc<Struct> as members for each of the dependent Structs. I thought of using Singleton Design Pattern to get the instance of that Struct in the impl block. This is a sample code that I've written. I would like to know if this code is safe, efficient and also a good way of modularization of the code.
use std::sync::{Arc, Mutex};
use lazy_static::lazy_static;
use std::thread;
struct A {
a: Mutex<i32>,
}
lazy_static! {
static ref A_OBJ: Arc<A> = Arc::new(A::new());
}
impl A {
fn new() -> A {
A {
a: Mutex::new(0),
}
}
pub fn print(&self) {
let mg = self.a.lock().unwrap();
println!("{}", mg);
}
pub fn increment_a(&self) {
let mut mg = self.a.lock().unwrap();
*mg += 1;
}
pub fn get_instance() -> &'static Arc<A> {
&A_OBJ
}
}
fn main() {
println!("Hello, world!");
let tjh = thread::spawn(|| {
println!("inside spawned thread");
let a_obj = A::get_instance();
a_obj.print();
a_obj.increment_a();
a_obj.print();
});
let a_obj = A::get_instance();
a_obj.print();
a_obj.increment_a();
a_obj.print();
let _ = tjh.join();
}