import threading mylock = threading.RLock(); num = 0 class myThread(threading.Thread): def __init__(self, name): threading.Thread.__init__(self,name=name) def run(self): global num while True: mylock.acquire() print('%s locked, Number: %d' % (threading.current_thread().name, num)) if num >= 4: mylock.release() print('%s released, Number: %d' % (threading.current_thread().name, num)) break num += 1 print('%s released, Number: %d' % (threading.current_thread().name, num)) mylock.release() if __name__ == '__main__': thread1 = myThread('Thread_1') thread2 = myThread('Thread_2') thread1.start() thread2.start()
If multiple threads of a common data modification, it might Chu is now unpredictable results, in order to properly insured
, it is necessary to synchronize multiple threads. Thread object using the Lock and RLock can achieve a simple thread synchronization. .
Both objects have methods relate acquire method and, for those data operation allows only one thread can
between its operation to acquire and release into the method.