Linux多线程锁属性设置方法

 更新时间:2017年01月05日 10:16:30   投稿:jingxian  
下面小编就为大家带来一篇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多线程锁属性设置方法全部内容了,希望大家多多支持脚本之家~

相关文章

  • Linux ls命令参数详解

    Linux ls命令参数详解

    下面是一个与 ls 一起使用的一些常用选项的简短列表。请记住,你可以通过阅读 ls 的说明书页(man ls)来获得选项的完整列表。
    2008-06-06
  • 教大家几种在Linux系统中查看时区的方法

    教大家几种在Linux系统中查看时区的方法

    Linux 包含多种可用的时间管理工具,比如 date 或 timedatectlcommands,你可以用它们来获取当前系统时区,也可以将系统时间与 NTP 服务器同步,来自动地、更精确地进行时间管理。下面通过这篇文章来一起看看在Linux系统中查看时区的几种不同方法。
    2017-01-01
  • ubuntu18.04安装搜狗拼音的简易教程

    ubuntu18.04安装搜狗拼音的简易教程

    这篇文章主要介绍了ubuntu18.04安装搜狗拼音的简易教程,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2018-05-05
  • CentOS7下安装yum源及上传下载命令rz、sz安装方法(图解)

    CentOS7下安装yum源及上传下载命令rz、sz安装方法(图解)

    这篇文章主要介绍了CentOS7下安装yum源及上传下载命令rz、sz安装方法,本文通过图文并茂的形式给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-11-11
  • 在Linux服务器中开放端口的三种方式

    在Linux服务器中开放端口的三种方式

    本文详细介绍了在Linux系统中通过firewall-cmd和iptables命令来开启防火墙并开放指定端口的方法,步骤包括启动防火墙、添加端口规则、重启防火墙以及检查端口状态,此外,还提供了两种不同的命令行方式来实现端口开放,需要的朋友可以参考下
    2025-02-02
  • 简单测试Apache是如何完成负载均衡策略配置

    简单测试Apache是如何完成负载均衡策略配置

    公司的系统在最初设计的时候就已经考虑到了负载均衡的规划,此次 就是对负载均衡的一个简单测试
    2014-01-01
  • Linux中一对多配置日志服务器的详细步骤

    Linux中一对多配置日志服务器的详细步骤

    这篇文章主要介绍了Linux中一对多配置日志服务器步骤教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • CentOS 7 安装 Jenkins过程详解

    CentOS 7 安装 Jenkins过程详解

    这篇文章主要介绍了CentOS 7 安装 Jenkins过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • linux安装jdk,tomcat 配置vsftp远程连接的步骤

    linux安装jdk,tomcat 配置vsftp远程连接的步骤

    这篇文章主要介绍了linux安装jdk,tomcat 配置vsftp远程连接,需要的朋友可以参考下
    2015-04-04
  • Linux系统设置tomcat开机自启介绍

    Linux系统设置tomcat开机自启介绍

    大家好,本篇文章主要讲的是Linux系统设置tomcat开机自启介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12

最新评论