python多线程编程4: 死锁和可重入锁

高洛峰
发布: 2016-10-18 11:31:53
原创
1543人浏览过

死锁

在线程间共享多个资源的时候,如果两个线程分别占有一部分资源并且同时等待对方的资源,就会造成死锁。尽管死锁很少发生,但一旦发生就会造成应用的停止响应。下面看一个死锁的例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

# encoding: UTF-8

import threading

import time

   

class MyThread(threading.Thread):

    def do1(self):

        global resA, resB

        if mutexA.acquire():

             msg = self.name+' got resA'

             print msg

                

             if mutexB.acquire(1):

                 msg = self.name+' got resB'

                 print msg

                 mutexB.release()

             mutexA.release()

    def do2(self):

        global resA, resB

        if mutexB.acquire():

             msg = self.name+' got resB'

             print msg

                

             if mutexA.acquire(1):

                 msg = self.name+' got resA'

                 print msg

                 mutexA.release()

             mutexB.release()

    

       

    def run(self):

        self.do1()

        self.do2()

resA = 0

resB = 0

   

mutexA = threading.Lock()

mutexB = threading.Lock()

   

def test():

    for i in range(5):

        t = MyThread()

        t.start()

if __name__ == '__main__':

    test()

登录后复制

执行结果:

Thread-1 got resA

Thread-1 got resB

立即学习Python免费学习笔记(深入)”;

Thread-1 got resB

立即学习Python免费学习笔记(深入)”;

Thread-1 got resA

Thread-2 got resA

Thread-2 got resB

Thread-2 got resB

Thread-2 got resA

Thread-3 got resA

Thread-3 got resB

Thread-3 got resB

Thread-3 got resA

Thread-5 got resA

Thread-5 got resB

Thread-5 got resB

Thread-4 got resA

此时进程已经死掉。

可重入锁

更简单的死锁情况是一个线程“迭代”请求同一个资源,直接就会造成死锁:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

import threading

import time

   

class MyThread(threading.Thread):

    def run(self):

        global num

        time.sleep(1)

   

        if mutex.acquire(1):

            num = num+1

            msg = self.name+' set num to '+str(num)

            print msg

            mutex.acquire()

            mutex.release()

            mutex.release()

num = 0

mutex = threading.Lock()

def test():

    for i in range(5):

        t = MyThread()

        t.start()

if __name__ == '__main__':

    test()

登录后复制

为了支持在同一线程中多次请求同一资源,python提供了“可重入锁”:threading.RLock。RLock内部维护着一个Lock和一个counter变量,counter记录了acquire的次数,从而使得资源可以被多次require。直到一个线程所有的acquire都被release,其他的线程才能获得资源。上面的例子如果使用RLock代替Lock,则不会发生死锁:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

import threading

import time

   

class MyThread(threading.Thread):

    def run(self):

        global num

        time.sleep(1)

   

        if mutex.acquire(1):

            num = num+1

            msg = self.name+' set num to '+str(num)

            print msg

            mutex.acquire()

            mutex.release()

            mutex.release()

num = 0

mutex = threading.RLock()

def test():

    for i in range(5):

        t = MyThread()

        t.start()

if __name__ == '__main__':

    test()

登录后复制

执行结果:

Thread-1 set num to 1

Thread-3 set num to 2

Thread-2 set num to 3

Thread-5 set num to 4

Thread-4 set num to 5

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号