首页 > 代码库 > python-Lock锁线程同步和互斥

python-Lock锁线程同步和互斥

 1  #!/usr/bin/python
 2 #coding=utf-8
 3 #线程间通信的同步与互斥操作-锁
 4 import threading
 5 a=b=0
 6 lock=threading.Lock()
 7 def value():
 8     while 1:
 9         lock.acquire()
10         if a!=b:
11             print "a=%d,b=%d"%(a,b)
12         lock.release()
13 t=threading.Thread(target=value)
14 t.start()
15 while True:
16     lock.acquire()
17     a+=1
18     b+=1
19     lock.release()

 

python-Lock锁线程同步和互斥