zoukankan      html  css  js  c++  java
  • python多线程编程(3): 死锁和可重入锁

    死锁

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

    按 Ctrl+C 复制代码
    按 Ctrl+C 复制代码

    执行结果:

    Thread-1 got resA
    Thread-1 got resB
    Thread-1 got resB
    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

    此时进程已经死掉。

    可重入锁

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

    复制代码
    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,则不会发生死锁:

    复制代码
    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

  • 相关阅读:
    ORACLE DBA的职责
    oracle开发常用LOV
    Oracle Patch 版本的查询
    指定二次分配为主要分配
    系统日期格式引起的错误:出生日期不能为将来日期
    分享一个帮助你检测网站颜色对比度的在线web工具 checkmycolours
    最常用的CURL命令大全
    超棒的javascript移动触摸设备开发类库 QUOjs
    分享一个超炫HTML5开发的jQuery进度条插件 percentageloader
    纯CSS实现的3D简洁按钮设计
  • 原文地址:https://www.cnblogs.com/lczit/p/4435798.html
Copyright © 2011-2022 走看看