我有一个问题,我需要x
线程等到它们都达到同步点。我的解决方案使用synchronise
下面的方法,每个线程函数在需要同步时调用该方法。
有一个更好的方法吗?
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