test_synchronization.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. use once_cell::sync::OnceCell;
  10. const N_THREADS: usize = 32;
  11. const N_ROUNDS: usize = 1_000_000;
  12. static CELLS: OnceCell<Vec<OnceCell<usize>>> = OnceCell::new();
  13. static RESULT: OnceCell<usize> = OnceCell::new();
  14. fn main() {
  15. let start = std::time::Instant::now();
  16. CELLS.get_or_init(|| vec![OnceCell::new(); N_ROUNDS]);
  17. let threads =
  18. (0..N_THREADS).map(|i| std::thread::spawn(move || thread_main(i))).collect::<Vec<_>>();
  19. for thread in threads {
  20. thread.join().unwrap();
  21. }
  22. println!("{:?}", start.elapsed());
  23. println!("No races detected");
  24. }
  25. fn thread_main(i: usize) {
  26. let cells = CELLS.get().unwrap();
  27. let mut accum = 0;
  28. for cell in cells.iter() {
  29. let &value = cell.get_or_init(|| i);
  30. accum += value;
  31. }
  32. assert_eq!(RESULT.get_or_init(|| accum), &accum);
  33. }