码迷,mamicode.com
首页 > 编程语言 > 详细

python 多线程-01 锁

时间:2020-03-09 15:09:46      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:counter   pen   main   def   star   targe   print   rlock   __name__   

import threading

# lock = threading.RLock()

# RLock 递归锁
lock = threading.RLock()  
Counter = [0]


def add(C):
    lock.acquire()
    C[0] = C[0] + 1
    lock.release()


if __name__ == '__main__':
    count = 0
    threads = []
    for i in range(10):
        t = threading.Thread(target=add, args=[Counter])
        threads.append(t)

    for t in threads:
        t.start()
        t.join()

    print(Counter)

python 多线程-01 锁

标签:counter   pen   main   def   star   targe   print   rlock   __name__   

原文地址:https://www.cnblogs.com/pythonPath/p/12448742.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!