PYTHON线程知识再研习D---可重入锁

不多解释,预防普通锁不正规的获取与释放

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import threading
import time

class MyThread(threading.Thread):
    def run(self):
        global num
        time.sleep(1)

        if mutex.acquire(1):
            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()
    t.join()
    print 'ALL DONE'

if __name__ == '__main__':
    test()
上一篇:IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别(附代码)


下一篇:PAT 1012 数字分类 C语言