1
我有这样的例子代码来解释我的问题:如何在运行前启动两个线程并锁定它们,并且只有在解锁时才执行?
import threading
import time
class thread1(threading.Thread):
def __init__(self, lock):
threading.Thread.__init__(self)
self.daemon = True
self.start()
self.lock = lock
def run(self):
while True:
self.lock.acquire(True)
print ('write done by t1')
self.lock.release()
class thread2(threading.Thread):
def __init__(self, lock):
threading.Thread.__init__(self)
self.daemon = True
self.start()
self.lock = lock
def run(self):
while True:
self.lock.acquire(True)
print ('write done by t2')
self.lock.release()
if __name__ == '__main__':
lock = threading.Lock()
t1 = thread1(lock)
t2 = thread2(lock)
lock.acquire(True)
counter = 0
while True:
print("main...")
counter = counter + 1
if(counter==5 or counter==10):
lock.release() # Here I want to unlock both threads to run just one time and then wait until I release again
time.sleep(1)
t1.join()
t2.join()
我遇到一些问题如下:
我想有两个线程(线程1和线程),它们在推出开始该程序的,但它们应该等到main()
counter
达到5或10
当main()
counter
达到5或10,但应信号/触发/解锁的螺纹,两者threads
应只运行一次,然后等到新的unlock
。
我期待的代码具有以下输出(每行是1秒运行):
main...
main...
main...
main...
main...
write done by t1
write done by t2
main...
main...
main...
main...
main...
write done by t1
write done by t2
相反,我有不同的行为,例如与起始:
write done by t1
write done by t1
write done by t1
write done by t1
(等)
并经过5秒
write done by t2
很多次...
有人可以帮我解释什么是错的,我该如何改善这一点?
真棒!谢谢你的解释!! – waas1919