def test_simple_barrier(num_of_threads): sb = SimpleBarrier(num_of_threads) threads = list() for i in range(num_of_threads): t = Thread(simple_barrier_example, sb, i) threads.append(t) for t in threads: t.join()
def test_reusable_barrier(num_of_threads): sb1 = SimpleBarrier(num_of_threads) sb2 = SimpleBarrier(num_of_threads) threads = list() for i in range(num_of_threads): t = Thread(reusable_barrier_example, sb1, sb2, f"Thread {i}") threads.append(t) for t in threads: t.join()
self.oxyQueue.signal() self.hydroQueue.signal(2) print("bonding") print("count after bond: oxygen: %d, hydrogen: %d\n" % (self.oxygenCount, self.hydrogenCount)) """ Vodík čaká, kým mu príde ďalší vodík a kyslík, cez prejdu dva. """ self.hydroQueue.wait() """ Bariera, ktorá pusti cez 3 prvky, aby nenastalo, že nejaký prvok sa stihne obehnúť. """ self.barrier.wait() threads = [] sh = Shared() for i in range(100): t = Thread(sh.oxygen) threads.append(t) for i in range(200): t = Thread(sh.hydrogen) threads.append(t) for t in threads: t.join()
while True: shared.mutex.lock() if shared.counter >= shared.end: shared.mutex.unlock() break shared.array[shared.counter] += 1 shared.counter += 1 shared.mutex.unlock() def counter3(shared): while True: shared.mutex.lock() cnt = shared.counter shared.counter +=1 shared.mutex.unlock() if cnt >= shared.end: break shared.array[cnt] += 1 for i in range(5): sh = Shared(1_000_000) t1 = Thread(counter3, sh) t2 = Thread(counter3, sh) t1.join() t2.join() print(Histogram(sh.array))
""" def computation(obj): obj.mutex.lock() while True: if obj.counter >= obj.end: break obj.elms[obj.counter] += 1 obj.counter += 1 obj.mutex.unlock() def histogram(array): unique = set(array) out = {} for item in unique: out[item] = array.count(item) return out shared = Shared(1000000) th1 = Thread(computation, shared) th2 = Thread(computation, shared) th1.join() th2.join() print(histogram(shared.elms))