多線程我有我需要x
線程等待,直到他們都達到了同步點的問題。我的解決方案使用下面的synchronise
方法,當它們需要同步時,它由每個線程函數調用。同步在python
有沒有更好的方式來做到這一點?
thread_count = 0
semaphore = threading.Semaphore()
event = threading.Event()
def synchronise(count):
""" All calls to this method will block until the last (count) call is made """
with semaphore:
thread_count += 1
if thread_count == count:
event.set()
event.wait()
def threaded_function():
# Do something
# Block until 4 threads have reached this point
synchronise(4)
# Continue doing something else
我探索你的第一個建議,但有一個需要有線程做兩個同步前和同步工作後不將努力分成2個任務。如果我沒有這個限制,你的解決方案將是理想的。 – 2009-05-20 13:05:59