class SimpleBarrierEvent: def __init__(self, num_of_threads): self.N = num_of_threads self.C = num_of_threads self.mutex = Mutex() self.turnstile = Event() def wait(self): self.mutex.lock() if self.C == 0: self.turnstile.clear() self.C += 1 if self.C == self.N: self.turnstile.set() self.mutex.unlock() self.turnstile.wait()
class ReusableBarrier: def __init__(self, numberOfThreads): self.numberOfThreads = numberOfThreads self.mutex = Mutex() self.event = Event() self.counter = 0 def wait(self): self.mutex.lock() if self.counter == 0: self.event.clear() self.counter += 1 if self.counter == self.numberOfThreads: self.event.signal() self.counter = 0 self.mutex.unlock() self.event.wait()