Linux多线程锁属性设置方法

 更新时间:2017年01月05日 10:16:30   投稿:jingxian  
下面小编就为大家带来一篇Linux多线程锁属性设置方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

互斥锁是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多线程锁属性设置方法全部内容了,希望大家多多支持程序员之家~

相关文章

  • Linux多线程编程快速入门

    Linux多线程编程快速入门

    这篇文章主要介绍了Linux多线程编程快速入门,涉及到了线程基本知识,线程标识,线程创建,线程终止,管理线程的终止等相关内容,小编觉得还是挺不错的,这里分享给大家,需要的朋友可以参考下
    2018-01-01
  • Linux下解压.tar.xz格式文件的方法

    Linux下解压.tar.xz格式文件的方法

    最近下载到了一份tar.xz结尾的压缩文件,网上解决的方法比较少,所以决定将解决的方法总结下来,这篇文章主要给大家介绍了在Linux下解压.tar.xz格式文件的方法,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-05-05
  • Linux统计一个文件中特定字符个数的方法

    Linux统计一个文件中特定字符个数的方法

    今天小编就为大家分享一篇关于Linux统计一个文件中特定字符个数的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • 关于Linux命令行下的数学运算示例详解

    关于Linux命令行下的数学运算示例详解

    在Linux中直接使用数学运算符进行数学运算往往得不到我们想要的计算结果。要在Shell中进行数学运算,我们需要借助点小手段。下面这篇文章主要给大家介绍了关于Linux命令行下的数学运算的相关资料,需要的朋友可以参考下
    2018-05-05
  • Linux中没有rc.local文件的完美解决方法

    Linux中没有rc.local文件的完美解决方法

    这篇文章主要介绍了Linux中没有rc.local文件的解决方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • Centos 7下利用crontab定时执行任务详解

    Centos 7下利用crontab定时执行任务详解

    这篇文章主要给大家介绍了关于Centos 7下利用crontab定时执行任务的相关资料,文中对crontab进行了详细的介绍,并给出了一些实例代码供大家参考学习,需要的朋友们下面跟着小编一起来学习学习吧。
    2017-06-06
  • Linux系统下如何创建普通用户并更改用户组

    Linux系统下如何创建普通用户并更改用户组

    这篇文章主要介绍了Linux系统下如何创建普通用户并更改用户组问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • Ubuntu下查看ssh服务是否安装或启动的方法

    Ubuntu下查看ssh服务是否安装或启动的方法

    这篇文章主要介绍了Ubuntu下查看ssh服务是否安装或启动的方法,文中给出了详细的方法示例,对大家具有一定的参考借鉴价值,有需要的朋友们下面来一起看看吧。
    2017-01-01
  • linux安装Jenkins超详细全过程

    linux安装Jenkins超详细全过程

    大家好,本篇文章主要讲的是linux安装Jenkins超详细全过程,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • CentOS6.5下Tomcat7 Nginx Redis配置步骤教程详解

    CentOS6.5下Tomcat7 Nginx Redis配置步骤教程详解

    这篇文章主要介绍了CentOS6.5下Tomcat7 Nginx Redis配置步骤,本文给大家介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下
    2016-10-10

最新评论

?


http://www.vxiaotou.com