use once_spin::*; use std::{ sync::{ mpsc::{channel, TryRecvError}, Arc, }, thread, }; fn use_barrier(n: usize, barrier: Arc) { let (tx, rx) = channel(); for _ in 0..n - 1 { let c = barrier.clone(); let tx = tx.clone(); thread::spawn(move || { tx.send(c.wait().is_leader()).unwrap(); }); } assert!(match rx.try_recv() { Err(TryRecvError::Empty) => true, _ => false, }); let mut leader_found = barrier.wait().is_leader(); for _ in 0..n - 1 { if rx.recv().unwrap() { assert!(!leader_found); leader_found = true; } } assert!(leader_found); } #[test] fn test_barrier() { const N: usize = 10; let barrier = Arc::new(Barrier::new(N)); use_barrier(N, barrier.clone()); use_barrier(N, barrier.clone()); }