https://stackoverflow.com/questions/4424193/what-happens-to-mutex-when-the-thread-which-acquired-it-exits?noredirect=1&lq=1

解释当一个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的状态,有两个办法:

  1. 调用pthread_mutex_consistent可以使得改mutex再次usable
  2. 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时会怎么样?的更多相关文章

  1. 线程系列08,实现线程锁的各种方式,使用lock,Montor,Mutex,Semaphore以及线程死锁

    当涉及到多线程共享数据,需要数据同步的时候,就可以考虑使用线程锁了.本篇体验线程锁的各种用法以及线程死锁.主要包括: ※ 使用lock处理数据同步※ 使用Monitor.Enter和Monitor.E ...

  2. C#使用Monitor类、Lock和Mutex类进行多线程同步

    在多线程中,为了使数据保持一致性必须要对数据或是访问数据的函数加锁,在数据库中这是很常见的,但是在程序中由于大部分都是单线程的程序,所以没有加锁的必要,但是在多线程中,为了保持数据的同步,一定要加锁, ...

  3. java中如何使正在运行中的线程退出

    终止线程的三种方法      有三种方法可以使终止线程.      1.  使用退出标志,使线程正常退出,也就是当run方法完成后线程终止.      2.  使用stop方法强行终止线程(这个方法不 ...

  4. C# lock关键词/lock语句块、线程锁

    一.lock关键词说明 1. lock 关键字将语句块标记为临界区,方法是获取给定对象的互斥锁,执行语句,然后释放该锁. 2. lock 语句块锁定,功能等同于 Monitor.Enter(obj): ...

  5. Pthreads并行编程之spin lock与mutex性能对比分析(转)

    POSIX threads(简称Pthreads)是在多核平台上进行并行编程的一套常用的API.线程同步(Thread Synchronization)是并行编程中非常重要的通讯手段,其中最典型的应用 ...

  6. Linux线程退出、资源回收、资源清理的方法

    首先说明线程中要回收哪些资源,理解清楚了这点之后在思考资源回收的问题. 1.子线程创建时从父线程copy出来的栈内存; 线程退出有多种方式,如return,pthread_exit,pthread_c ...

  7. WCF 遇到 由于线程退出或应用程序请求,已放弃 I/O 操作 ListenerContextInputStream

    异常类型:IOException 异常消息:An exception has been thrown when reading the stream. 异常信息: at System.ServiceM ...

  8. windows主线程等待子线程退出卡死问题

    在windows下调用_beginthread创建子线程并获得子线程id(函数返回值),如果子线程很快退出,在主线程中调用WaitForSingleObject等待该线程id退出,会导致主线程卡死.需 ...

  9. 【Java先进】Lock、通过使用线程池

    import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util ...

随机推荐

  1. 接口测试心得--签名处理(Python)

    一.背景 最近负责的项目接口签名规则做了调整,第一次接触“2次认证“的方式,正好有时间,记录一下. 测试的服务A有一部分接口需要给第三方调用,这样需要对第三方有个认证,认证是由一个公共服务(API鉴权 ...

  2. Kubernetes集群部署史上最详细(二)Prometheus监控Kubernetes集群

    使用Prometheus监控Kubernetes集群 监控方面Grafana采用YUM安装通过服务形式运行,部署在Master上,而Prometheus则通过POD运行,Grafana通过使用Prom ...

  3. Springboot文件上传代码笔记

    1.在src下创建filter包,包内Class名UploadFilter package com.gd.filter; import org.apache.catalina.servlet4prev ...

  4. 使用ConcurrentHashMap一定线程安全?

    前言 老王为何半夜惨叫?几行代码为何导致服务器爆炸?说好的线程安全为何还是出问题?让我们一起收看今天的<走进IT> 正文 CurrentHashMap出现背景 说到ConcurrentHa ...

  5. 4.JAVA-数组、String详解

    1.数组 public class Test{ public static void main(String args[]){ int[] intArray = new int[] {1,4,3,2, ...

  6. 数据库艰难求生之路(基础:增删改查)part2

    一.数据库查询 由于这个点的东西实在是多的,我就和题目,知识点一起演示. 首先是创建数据库: create database ExampleInfo --创建数据库 use ExampleInfo - ...

  7. Linux基本操作——文件相关

    一.前言 无论是IC工程师.FPGA工程师还是嵌入式软件工程师,都或多或少会接触到Linux操作系统.有很多EDA工具只有Linux版本,因此掌握基本的操作和常用命令十分必要.Linux中的数据均以文 ...

  8. NPOI插入图片到excel指定单元格

    先看效果图 下载NPOI组件(2.0以上支持.xlsx和.xls的excel,2.0以下只支持.xls) NPOI下载官网http://npoi.codeplex.com 下载解压,里面有个dotne ...

  9. Netty3:分隔符和定长解码器

    回顾TCP粘包/拆包问题解决方案 上文详细说了TCP粘包/拆包问题产生的原因及解决方式,并以LineBasedFrameDecoder为例演示了粘包/拆包问题的实际解决方案,本文再介绍两种粘包/拆包问 ...

  10. Redis常用数据类型和事物以及并发

    Redis数据类型 基本类型(String int): 如 set key value .get key 等 所有命令都是按照 key value keys * 可以将全部数据列出,其中后面的 &qu ...