pyhton 线程锁
问题:已经有了全局解释器锁为什么还需要锁?
答:全局解释器锁是在Cpython解释器下,同一时刻,多个线程只能有一个线程被cpu调度
它是在线程和cpu之间加锁,线程和cpu之间有传递时间,即使有GIL,也无法保证数据的绝对安全
锁的分类
1、互斥锁
2、死锁
3、递归锁
# 虽然有全局解释器锁,数据仍然出现了安全问题
from threading import Thread
import time def test():
global n
temp = n
time.sleep(1)
n = temp - 1 n = 10
t_li = []
for i in range(5):
t = Thread(target=test)
t_li.append(t)
t.start()
[t.join() for t in t_li]
print(n) #
互斥锁
# 使用锁,解决了数据安全问题
from threading import Thread, Lock
import time def test(lock):
lock.acquire()
global n
temp = n
time.sleep(1)
n = temp - 1
lock.release() n = 10
lock = Lock()
t_li = []
for i in range(5):
t = Thread(target=test, args=(lock, ))
t_li.append(t)
t.start()
[t.join() for t in t_li]
print(n) #
死锁
# 科学家吃面
from threading import Lock
from threading import Thread
import time
noddle = Lock()
chopsticks = Lock() def test1(name):
noddle.acquire()
print('%s拿到面条' % name)
# time.sleep(2)
chopsticks.acquire()
print('%s拿到筷子' % name)
print('%s吃面' % name)
# time.sleep(1)
chopsticks.release()
noddle.release() def test2(name):
chopsticks.acquire()
print('%s拿到筷子' % name)
time.sleep(0.3)
noddle.acquire()
print('%s拿到面条' % name)
print('%s吃面' % name)
noddle.release()
chopsticks.release() t1 = Thread(target=test1, args=('tom', ))
t1.start()
t2 = Thread(target=test2, args=('abc', ))
t2.start() t3 = Thread(target=test1, args=('joker', ))
t3.start()
t4 = Thread(target=test2, args=('ff', ))
t4.start()
递归锁
# 递归锁,多个acquire()不会造成死锁
from threading import RLock
from threading import Thread
a = RLock() def test():
a.acquire()
a.acquire()
a.acquire()
print('hello, world') Thread(target=test).start()
# 科学家吃面 递归锁
from threading import RLock
from threading import Thread
import time
noddle = chopsticks = RLock() def test1(name):
noddle.acquire()
print('%s拿到面条' % name)
# time.sleep(2)
chopsticks.acquire()
print('%s拿到筷子' % name)
print('%s吃面' % name)
# time.sleep(1)
chopsticks.release()
noddle.release() def test2(name):
chopsticks.acquire()
print('%s拿到筷子' % name)
time.sleep(0.3)
noddle.acquire()
print('%s拿到面条' % name)
print('%s吃面' % name)
noddle.release()
chopsticks.release() t1 = Thread(target=test1, args=('tom', ))
t1.start()
t2 = Thread(target=test2, args=('abc', ))
t2.start()
t3 = Thread(target=test1, args=('joker', ))
t3.start()
t4 = Thread(target=test2, args=('ff', ))
t4.start()
pyhton 线程锁的更多相关文章
- NSLock线程锁的使用测试
测试1:NSLock线程锁是不是单例? 打印: 结论1:NSLock不是单例 测试2:同一个线程锁在不同的地方锁定,是否会有锁定两个? 打印为: 结论2:顺序打印,在不同的地方锁定也可以锁定. 测试3 ...
- day9---多线程,线程锁,队列
进程.线程 http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html 使用threading模块实现多线程编程[综述] Pyt ...
- python线程锁
import time,threading balance = 0 lock = threading.Lock() def change_it(n): global balance balance = ...
- linux下使用线程锁互斥访问资源
linux使用线程锁访问互斥资源: 1.线程锁的创建 pthread_mutex_t g_Mutex; 2.完整代码如下 #include <stdio.h> #include <s ...
- JAVA线程锁-读写锁
JAVA线程锁,除Lock的传统锁,又有两种特殊锁,叫读写锁ReadWriteLock 其中多个读锁不互斥,读锁和写锁互斥,写锁和写锁互斥 例子: /** * java线程锁分为读写锁 ReadWri ...
- Java线程锁一个简单Lock
/** * @author * * Lock 是java.util.concurrent.locks下提供的java线程锁,作用跟synchronized类似, * 单是比它更加面向对象,两个线程执行 ...
- python_way ,day11 线程,怎么写一个多线程?,队列,生产者消费者模型,线程锁,缓存(memcache,redis)
python11 1.多线程原理 2.怎么写一个多线程? 3.队列 4.生产者消费者模型 5.线程锁 6.缓存 memcache redis 多线程原理 def f1(arg) print(arg) ...
- Linux同步机制(一) - 线程锁
1 互斥锁 在线程实际运行过程中,我们经常需要多个线程保持同步. 这时可以用互斥锁来完成任务.互斥锁的使用过程中,主要有 pthread_mutex_init pthread_mutex_destor ...
- 单例模式——使用GCD实现单例模式 & 非ARC单例模式 &使用GCD和线程锁实现单例模式-b
1.单利模式概述 链接: iOS开发懒汉模式&恶寒模式 2.使用GCD实现单利模式 2.1新建一个project,然后新建一个HMDataTool类展示GCD实现单例模式 #import & ...
随机推荐
- 几种常见排序算法的基本介绍,性能分析,和c语言实现
本文介绍6种常见的排序算法,以及他们的原理,性能分析和c语言实现: 为了能够条理清楚,本文所有的算法和解释全部按照升序排序进行 首先准备一个元素无序的数组arr[],数组的长度为length,一个交换 ...
- H3C DNS简介
- Team Foundation Server 2015使用教程【6】:新增权限为读取器的团队
- FreeNOS学习2——操作系统是如何启动的
The System Boot Process Explained:https://www.webopedia.com/DidYouKnow/Hardware_Software/BootProcess ...
- LeetCode111_求二叉树最小深度(二叉树问题)
题目: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the s ...
- (转)hibernate缓存机制详细分析
在本篇随笔里将会分析一下hibernate的缓存机制,包括一级缓存(session级别).二级缓存(sessionFactory级别)以及查询缓存,当然还要讨论下我们的N+1的问题. 随笔虽长,但我相 ...
- 博客同步到CSDN客户端
同步本人博客到CSDN客户端 http://blog.csdn.net/johnnyz1234
- 用postman验证接口是否可掉通
1.结合fidder抓包工具 2.打开postman 3.点击Launchpad右边“+” 4.选择postman,url粘贴fidder抓出来的数据 5.Header中粘贴fidder抓出来的KEY ...
- Java面试思路
一.javaSE基础 1.java IO流 2.java NIO 3.java集合 4.java注解 5.java泛型 6.java反射 7.java多线程 8.常用String.数组.日期操作 二. ...
- c++简单实现二叉树
专业术语: 节点 父节点 根节点 子孙 堂兄弟 深度: 从根节点到最底层节点的层数称为深度 叶子节点: 没有子节点的节点称为叶子节点 非终端节点: 实际就是非叶子节点 度: 子节点的个数称为度 树的分 ...