1 | //! Test if the OnceCell properly synchronizes. |
2 | //! Needs to be run in release mode. |
3 | //! |
4 | //! We create a `Vec` with `N_ROUNDS` of `OnceCell`s. All threads will walk the `Vec`, and race to |
5 | //! be the first one to initialize a cell. |
6 | //! Every thread adds the results of the cells it sees to an accumulator, which is compared at the |
7 | //! end. |
8 | //! All threads should end up with the same result. |
9 | |
10 | use once_cell::sync::OnceCell; |
11 | |
12 | const N_THREADS: usize = 32; |
13 | const N_ROUNDS: usize = 1_000_000; |
14 | |
15 | static CELLS: OnceCell<Vec<OnceCell<usize>>> = OnceCell::new(); |
16 | static RESULT: OnceCell<usize> = OnceCell::new(); |
17 | |
18 | fn main() { |
19 | let start = std::time::Instant::now(); |
20 | CELLS.get_or_init(|| vec![OnceCell::new(); N_ROUNDS]); |
21 | let threads = |
22 | (0..N_THREADS).map(|i| std::thread::spawn(move || thread_main(i))).collect::<Vec<_>>(); |
23 | for thread in threads { |
24 | thread.join().unwrap(); |
25 | } |
26 | println!("{:?}" , start.elapsed()); |
27 | println!("No races detected" ); |
28 | } |
29 | |
30 | fn thread_main(i: usize) { |
31 | let cells = CELLS.get().unwrap(); |
32 | let mut accum = 0; |
33 | for cell in cells.iter() { |
34 | let &value = cell.get_or_init(|| i); |
35 | accum += value; |
36 | } |
37 | assert_eq!(RESULT.get_or_init(|| accum), &accum); |
38 | } |
39 | |