最简单来说,假如有3个线程同时访问一个全局变量,那么很可能会操作互斥错误的情况,代码如下:
#!/usr/bin/python
#encoding=utf-8
import time
import threading
index = 0
def printIndex():
global index
global lock
while index < 100:
print index,
index += 1
time.sleep(1)
t1 = threading.Thread(target = printIndex, args=())
t1.setDaemon(True)
t1.start()
t2 = threading.Thread(target = printIndex, args=())
t2.setDaemon(True)
t2.start()
t3 = threading.Thread(target = printIndex, args=())
t3.setDaemon(True)
t3.start()
time.sleep(10)
在没有互斥的情况下3个线程同时访问一个全局变量,最后结果如下:
由此可见,输出的完全是乱码,因为这个地方除了全局变量是临界资源外控制台也是一个临界资源。所以需要互斥操作。将程序改为:
#!/usr/bin/python
#encoding=utf-8
import time
import threading
index = 0
lock = threading.RLock()
def printIndex():
global index
global lock
while index < 100:
lock.acquire()
print(index),
index += 1
lock.release()
time.sleep(1)
t1 = threading.Thread(target = printIndex, args=())
t1.setDaemon(True)
t1.start()
t2 = threading.Thread(target = printIndex, args=())
t2.setDaemon(True)
t2.start()
t3 = threading.Thread(target = printIndex, args=())
t3.setDaemon(True)
t3.start()
time.sleep(100)
最后输出结果:
由此可见,输出时完全按顺序的。在代码中先用RLock()创建一个锁,当需要访问临界资源时进行加锁lock.require(),等退出时再释放锁lock.release()。
对于临界资源来说,用锁可以很方便实现互斥,而如果要实现同步,则需要用event。
假如程序中需要进行复杂的计算,现在将这计算在另一线程中运行,但是主程序怎么才能知道计算完成了呢?实例代码如下:
import time
import threading
def getSum(n, sum):
for i in range(0, n + 1):
sum[0] += i
time.sleep(0.1)
def main():
sum = [0]
t = threading.Thread(target = getSum, args=(100, sum,))
t.setDaemon(True)
t.start()
time.sleep(2)
print('sum = ', sum[0])
if __name__ == '__main__':
main()
现在需要求1 + 2 + 3 + ... + N 的和,计算放在单独线程中进行,但是我们不知道计算什么时候结束,只能假设2s,最后结果如下:
最后结果sum = 153,这显然是不正确的,即使是正确,也是巧合。这时就需要一个类似于信号的东西,让子进程结束后能够通过这个信号告诉主进程,event就可以配上用
场了。event和lock的用法差不多,下面是代码:
import time
import threading
def getSum(n, sum, event):
for i in range(0, n + 1):
sum[0] += i
time.sleep(0.1)
event .set()
def main():
sum = [0]
event = threading.Event()
t = threading.Thread(target = getSum, args=(100, sum, event))
t.setDaemon(True)
t.start()
event.wait()
print('sum = ', sum[0])
if __name__ == '__main__':
main()
程序中用了event作为一个信号量,初始是没信号的,当子进程完成计算后将其设置为有信号,主进程wait返回,最后结果:
最后,写个生产者和消费者的模型吧:
import time
import random
import threading
def producter(buffer, event):
while True:
event.wait()
event.clear()
if len(buffer) > 4:
print("the buffer is full! producter wait...")
else:
data = random.randint(1, 10)
print('the producter has put data: ', data)
buffer.append(data)
event.set()
time.sleep(0.8)
pass
def consumer(buffer, event):
while True:
event.wait()
event.clear()
if len(buffer) == 0:
print('he buffer is empty! consumer wait...')
else:
data = buffer[0]
del buffer[0]
print('the consumer has get data:', data)
event.set()
time.sleep(0.4)
def main():
buffer = []
event = threading.Event()
event.set()
p = threading.Thread(target = producter, args=(buffer, event))
p.setDaemon(True)
p.start()
c = threading.Thread(target = consumer, args=(buffer, event))
c.setDaemon(True)
c.start()
if __name__ == '__main__':
main()
time.sleep(100)
这儿有个问题,这里是wait之后再手工clear的,那么这中间就有可能出现线程切换,有没有方法在wait返回后自动clear的呢??