我有两段代码:
section_a
# some code
section_b
仅当执行section_b时,我才需要阻止section_a。如果section_b中没有块,则section_a可以由多个线程执行。
当section_b开始执行时,我需要向其他线程发送一个信号,不要进入section_a(在它之前等待)。当最后一个线程离开section_a时,必须执行section_b 。
如果我使用条件锁定 (threading.Condition.acquire(), threading.Condition.release()) 来解决问题,我很困惑如何允许多个线程同时进入section_a。当我们在section_a之前调用 condition.acquire() 时,其他线程也会被阻塞以进入该部分。
while True:
condition.acquire() # other threads that would enter point_a can't do that after this acquire()
#section_a, point_a
condition.release()
# another place of a program
while True:
condition.acquire() # I'd like threads can't enter section_a after this acquire, but not after acquire before point_a
#section_b
condition.release()
如果section_a由任何线程执行,我需要阻止section_b 。
你能告诉我正确的结构吗?
执行我,我对多线程没有经验。可能有一个完全不同的结构。