Home
Rust
OnceLock Example (static)
This page was last reviewed on Jun 21, 2023.
Dot Net Perls
OnceLock. Often in multithreaded Rust programs we want to initialize some data that is important for more than one thread. And it only needs to be initialized once, not many times.
With OnceLock, we can have clear syntax for a struct that is only created once, but shared multiple times. Creation occurs on a thread—other work can be done in parallel.
Mutex
OnceCell
Example. To begin we have a struct called Test—it holds some data that requires initialization. A string must be created. A Test struct is contained in a static OnceLock.
Start In main() we create 8 threads and place them in a vector so they can later be joined together.
Then In the closure inside thread spawn() we call get_or_init() on the TEST OnceLock.
And The get_or_init() function lazily invokes the init_test() function. This is only called once.
Result In this program, one thread creates a Test struct and stores it in the OnceLock, and then every other thread accesses that static data.
use std::sync::OnceLock; use std::sync::*; use std::thread; static TEST: OnceLock<Test> = OnceLock::new(); struct Test { name: String, count: usize, } fn init_test() -> Test { // Logic to initialize the static data in the OnceLock. println!("Initializing test"); Test { name: String::from("BIRD"), count: 500, } } fn main() { let mut children = vec![]; for _ in 0..8 { children.push(thread::spawn(move || { // Initialize the data on the first access. // If already initialized, just return the existing data. let test = TEST.get_or_init(|| init_test()); println!("TEST: {}", test.name); })); } for child in children { let _result = child.join(); } println!("DONE"); }
Initializing test TEST: BIRD TEST: BIRD TEST: BIRD TEST: BIRD TEST: BIRD TEST: BIRD TEST: BIRD TEST: BIRD DONE
With OnceLock and the similar type OnceCell (which is for non-threaded scenarios) we have a lazy-instantiation type. OnceLock is safe on multiple threads—work can be done in parallel.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 21, 2023 (edit link).
Home
Changes
© 2007-2024 Sam Allen.