lock了mutex的线程退出了却没有unlock时会怎么样?
解释当一个lock了mutex的线程退出了,却没有主动unlock这个mutex时,会发生什么
If you created a robust mutex by setting up the right attributes before calling pthread_mutex_init, the mutex will enter a special state when the thread that holds the lock terminates, and the next thread to attempt to acquire the mutex will obtain an error of EOWNERDEAD. It is then responsible for cleaning up whatever state the mutex protects and calling pthread_mutex_consistent to make the mutex usable again, or calling pthread_mutex_unlock (which will make the mutex permanently unusable; further attempts to use it will return ENOTRECOVERABLE).
假设在pthread_mutex_init的时候,通过设置合适的属性得到一个robust mutex,当一个拥有该mutex的线程exit,并且没有unlock这个mutex的时候,该mutex就会进入一个specail state。另外一个线程想要lock这个mutex的时候,会返回一个EOWNERDEAD错误。这个时候就需要清除该mutex的状态,有两个办法:
- 调用pthread_mutex_consistent可以使得改mutex再次usable
- pthread_mutex_unlock使得该mutex永久unusable,尝试再次使用该mutex的将会得到ENOTRECOVERABLE错误
For non-robust mutexes, the mutex is permanently unusable if the thread that locked it terminates without unlocking it. Per the standard (see the resolution to issue 755 on the Austin Group tracker), the mutex remains locked and its formal ownership continues to belong to the thread that exited, and any thread that attempts to lock it will deadlock. If another thread attempts to unlock it, that's normally undefined behavior, unless the mutex was created with the PTHREAD_MUTEX_ERRORCHECK attribute, in which case an error will be returned.
对non-robust mutexes,如果持有锁的线程退出且没有unlock该锁,那么该mutex将会永久不可以使用。这个mutex会保持被已经退出的该线程持有lock的状态,任何尝试lock这个mutex的线程将陷入deadlock状态。如果任何线程尝试unlock这个mutex,那么将会产生undefined behavior。但如果mutex创建的时候设置了PTHREAD_MUTEX_ERRORCHECK属性,那么unlock将会返回错误。
On the other hand, many (most?) real-world implementations don't actually follow the requirements of the standard. An attempt to lock or unlock the mutex from another thread might spuriously succeed, since the thread id (used to track ownership) might have been reused and may now refer to a different thread (possibly the one making the new lock/unlock request). At least glibc's NPTL is known to exhibit this behavior.
但是,很多real-world的实现版本却没有满足standard的要求。由于thread id可能会被重用,所以,有时候lock或者unlock会产生不合逻辑的成功,比如一个线程A lock了某个mutex,然后该A线程id给别的线程B用了,那么线程B去unlock也会成功。比如glibc's NPTL就会表现出这样的行为。
lock了mutex的线程退出了却没有unlock时会怎么样?的更多相关文章
- 线程系列08,实现线程锁的各种方式,使用lock,Montor,Mutex,Semaphore以及线程死锁
当涉及到多线程共享数据,需要数据同步的时候,就可以考虑使用线程锁了.本篇体验线程锁的各种用法以及线程死锁.主要包括: ※ 使用lock处理数据同步※ 使用Monitor.Enter和Monitor.E ...
- C#使用Monitor类、Lock和Mutex类进行多线程同步
在多线程中,为了使数据保持一致性必须要对数据或是访问数据的函数加锁,在数据库中这是很常见的,但是在程序中由于大部分都是单线程的程序,所以没有加锁的必要,但是在多线程中,为了保持数据的同步,一定要加锁, ...
- java中如何使正在运行中的线程退出
终止线程的三种方法 有三种方法可以使终止线程. 1. 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止. 2. 使用stop方法强行终止线程(这个方法不 ...
- C# lock关键词/lock语句块、线程锁
一.lock关键词说明 1. lock 关键字将语句块标记为临界区,方法是获取给定对象的互斥锁,执行语句,然后释放该锁. 2. lock 语句块锁定,功能等同于 Monitor.Enter(obj): ...
- Pthreads并行编程之spin lock与mutex性能对比分析(转)
POSIX threads(简称Pthreads)是在多核平台上进行并行编程的一套常用的API.线程同步(Thread Synchronization)是并行编程中非常重要的通讯手段,其中最典型的应用 ...
- Linux线程退出、资源回收、资源清理的方法
首先说明线程中要回收哪些资源,理解清楚了这点之后在思考资源回收的问题. 1.子线程创建时从父线程copy出来的栈内存; 线程退出有多种方式,如return,pthread_exit,pthread_c ...
- WCF 遇到 由于线程退出或应用程序请求,已放弃 I/O 操作 ListenerContextInputStream
异常类型:IOException 异常消息:An exception has been thrown when reading the stream. 异常信息: at System.ServiceM ...
- windows主线程等待子线程退出卡死问题
在windows下调用_beginthread创建子线程并获得子线程id(函数返回值),如果子线程很快退出,在主线程中调用WaitForSingleObject等待该线程id退出,会导致主线程卡死.需 ...
- 【Java先进】Lock、通过使用线程池
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util ...
随机推荐
- ab性能测试工具的使用
一.什么是ab ab,即Apache Benchmark,是一种用于测试Apache超文本传输协议(HTTP)服务器的工具. ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL地址进行访 ...
- 『性能』ServiceStack.Redis 和 StackExchange.Redis 性能比较
背景 近来,需要用到 Redis 这类缓存技术 —— MongoDB 和 Redis 没有进行过比较. 我也懒得在这些细节上 纠结那么多 —— 按照网友给出的文章,听从网友建议,选择 Redis. R ...
- 《阿里巴巴 Java开发手册》读后感
前言 只有光头才能变强 前一阵子一直在学Redis,结果在黄金段位被虐了,暂时升不了段位了,每天都拿不到首胜(好烦). 趁着学校校运会,合理地给自己放了一个小长假,然后就回家了.回到家才发现当时618 ...
- javascript入门篇(五)
将日期转换为数字 全局方法 Number() 可将日期转换为数字 d = new Date(); Number(d) // 返回 1404568027739 日期方法 getTime ...
- AI繁荣下的隐忧——Google Tensorflow安全风险剖析
本文由云+社区发表 作者:[ Tencent Blade Team ] Cradmin 我们身处一个巨变的时代,各种新技术层出不穷,人工智能作为一个诞生于上世纪50年代的概念,近两年出现井喷式发展,得 ...
- asp.net core 系列之中间件进阶篇-编写自定义中间件(middleware)
中间件是被用到管道(pipeline)上来处理请求(request)和响应的(response). asp.net core 本身提供了一些内置的中间件,但是有一些场景,你可能会需要写一些自定义的中间 ...
- PageHelper分页异常(java.base/java.util.ArrayList cannot be cast to com.github.pagehelper.Page)
在SqlMapConfig.xml里面配置分页插件 applicationContext-service.xml里面的配置,我出现问题谁因为,在salSessionFactory里没注入全局配置文件
- 搭建基于SornaQube的自动化安全代码检测平台
一.背景和目的 近年来,随着新业务.新技术的快速发展,应用软件安全缺陷层出不穷.虽然一般情况下,开发者基本都会有单元测试.每日构建.功能测试等环节来保证应用的可用性.但在安全缺陷方面,缺乏安全意识.技 ...
- C#中关于表达式与委托在EF中的不同表现总结
Func<Invoice, bool> func = x => x.State == InvoiceState.Created; Expression<Func<Invo ...
- 通信(二):进程间通信之socket
一.为什么要学习socket? 我们打开浏览器浏览网页时,浏览器的进程怎么与web服务器通信的?我们用QQ聊天时,QQ进程怎么与服务器或你好友所在的QQ进程通信?这些都得靠socket.本地的进程间通 ...