来自
implementation
属于
wait()
:
pub fn wait(&self) -> BarrierWaitResult {
let mut lock = self.lock.lock().unwrap();
let local_gen = lock.generation_id;
lock.count += 1;
if lock.count < self.num_threads {
let _guard =
self.cvar.wait_while(lock, |state| local_gen == state.generation_id).unwrap();
BarrierWaitResult(false)
} else {
lock.count = 0;
lock.generation_id = lock.generation_id.wrapping_add(1);
self.cvar.notify_all();
BarrierWaitResult(true)
}
}
它似乎保留了所有线程的计数。当一个给定的线程聚合时,如果它是最后一个线程,它将成为前导线程(
BarrierWaitResult(true)
),否则它将成为非领导者(
BarrierWaitResult(false)
).