有人可以告诉我,如果以下线程安全与否,以及如果它不是我必须做什么才能使它如此。蟒蛇线程上的线程安全锁
注意:这只是一个小样本,不知道它是否运行。
TIMER = True
time_lock = threading.Lock()
def timing():
while TIMER:
# some logic will be here for now print time
print time.time()
timer = threading.Thread(target=timing)
timer2 = threading.Thread(target=timing)
timer.start()
timer2.start()
while True:
time_lock.aquire()
if doSomeStuff():
TIMER = True
if otherThings():
break
time_lock.aquire()
TIMER = False
time_lock.release()
time_lock.aquire()
TIMER = False
time_lock.release()
什么时候一个人可以写作,一个人可以同时阅读?或者这会不会发生? –
@aspect它并不重要 - 因为你只是改变一个布尔False为真,没有“临时”写入状态,其他线程可以ee – katrielalex