当前位置: 首页 > 编程笔记 >

Linux多线程锁属性设置方法

邓兴为
2023-03-14
本文向大家介绍Linux多线程锁属性设置方法,包括了Linux多线程锁属性设置方法的使用技巧和注意事项,需要的朋友参考一下

互斥锁是Linux下多线程资源保护的常用手段,但是在时序复杂的情况下,很容易会出现死锁的情况。

可以通过设置锁的属性,避免同一条线程重复上锁导致死锁的问题。

通过int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)接口设置

一般是以下四种属性:

PTHREAD_MUTEX_NORMAL
This type of mutex does not detect deadlock. A thread attempting to relock this mutex without first unlocking it will deadlock. Attempting to unlock a mutex locked by a different thread results in undefined behaviour. Attempting to unlock an unlocked mutex results in undefined behaviour.

PTHREAD_MUTEX_ERRORCHECK
This type of mutex provides error checking. A thread attempting to relock this mutex without first unlocking it will return with an error. A thread attempting to unlock a mutex which another thread has locked will return with an error. A thread attempting to unlock an unlocked mutex will return with an error.

PTHREAD_MUTEX_RECURSIVE
A thread attempting to relock this mutex without first unlocking it will succeed in locking the mutex. The relocking deadlock which can occur with mutexes of type PTHREAD_MUTEX_NORMAL cannot occur with this type of mutex. Multiple locks of this mutex require the same number of unlocks to release the mutex before another thread can acquire the mutex. A thread attempting to unlock a mutex which another thread has locked will return with an error. A thread attempting to unlock an unlocked mutex will return with an error.

PTHREAD_MUTEX_DEFAULT
Attempting to recursively lock a mutex of this type results in undefined behaviour. Attempting to unlock a mutex of this type which was not locked by the calling thread results in undefined behaviour. Attempting to unlock a mutex of this type which is not locked results in undefined behaviour. An implementation is allowed to map this mutex to one of the other mutex types.

这里主要指同一条线程重复上锁,不同线程上锁,无论设置什么属性,当锁已经被锁定后都会互斥阻塞。

使用PTHREAD_MUTEX_RECURSIVE属性,当同一条线程在没有解锁的情况下尝试再次锁定会返回成功。

以上就是小编为大家带来的Linux多线程锁属性设置方法全部内容了,希望大家多多支持小牛知识库~

 类似资料:
  • FLTK支持设定线条的宽度和类型。 void fl_line_style(int style, int width=0, char* dashes=0) style是以下几种类型之一,默认的是FL_SOLID。 FL_SOLID ------- FL_DASH - - - - FL_DOT ....... FL_DASHDOT - . - . FL_DASHDOTDOT - .. - FL_CA

  • 问题内容: 我正在努力加快某些过程的执行速度,这些过程将大量记录(大多数是几百万个)发布到Elasticsearch。在我的C#代码中,我已经使用Dataflow实现了一个多线程解决方案,如下所示: 然后我要实现的发送批量请求调用: 我的问题 ,你 是对的实用性存在的数据流管道的一部分的锁内执行额外的线程。 这个可以吗?我可以在性能,执行,缓存/内存丢失等方面看到任何潜在的问题吗? 任何见识都会很

  • 所以我需要为@Transactional注释设置timeout参数。这个属性将来自一个属性文件,我不能这样做,因为我遇到了“注释属性transactional.timeout的值必须是常量表达式”。像这样的东西

  • 问题内容: 如何确保从hibernate.cfg.xml加载所有属性,然后以编程方式添加其他属性?我看到了以下代码片段,但它看起来像是全新的配置,而不是现有配置的补充。 问题答案: 您显示的代码段是您所需要的。只需使用您现有的配置,而不是创建一个新的配置即可。 如果不是您实例化配置(例如,spring),则需要扩展创建它的类。

  • 本文向大家介绍vue绑定设置属性的多种方式(5),包括了vue绑定设置属性的多种方式(5)的使用技巧和注意事项,需要的朋友参考一下 vue系列教程第五篇,即绑定设置属性的多种方式,具体内容如下 一、设置属性的值: {{data中的数据}} 二、v-bind绑定属性的值 三、简写方式,冒号 (:) 绑定 四、绑定多个属性 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 主要内容:Jsoup 设置属性 语法,Jsoup 设置属性 说明,Jsoup 设置属性 示例以下示例将展示在将 HTML 字符串解析为 Document 对象后使用方法设置 dom 元素的属性、批量更新和添加/删除类方法。 Jsoup 设置属性 语法 document : 文档对象代表 HTML DOM。 Jsoup : 解析给定 HTML 字符串的主类。 html : HTML 字符串。 link : 元素对象表示表示锚标记的 html 节点元素。 link.attr() : attr