多线程我有我需要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