全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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  # 多线程锁属性  # linux 可执行文件与写操作的同步问题(文件读写操作产生的锁机制)  # 浅析Linux下一个简单的多线程互斥锁的例子  # linux shell之文件锁  # 详解Linux文件锁flock  # linux中各种锁机制的使用与区别详解  # 死锁  # 多线程  # 情况下  # 互斥  # 很容易  # 可以通过  # 四种  # 小编  # 大家多多  # 解锁  # 但是在  # calling  # pre  # class  # strong  # 一般是  # brush  # detect  # deadlock  # thread 


相关文章: 宝塔建站后网页无法访问如何解决?  代购小票制作网站有哪些,购物小票的简要说明?  高端建站三要素:定制模板、企业官网与响应式设计优化  建站之星体验版:智能建站系统+响应式设计,多端适配快速建站  制作网站公司那家好,网络公司是做什么的?  ,如何利用word制作宣传手册?  ,南京靠谱的征婚网站?  php条件判断怎么写_ifelse和switchcase的使用区别【对比】  如何快速选择适合个人网站的云服务器配置?  微信h5制作网站有哪些,免费微信H5页面制作工具?  如何通过VPS搭建网站快速盈利?  建站之星后台管理:高效配置与模板优化提升用户体验  网站app免费制作软件,能免费看各大网站视频的手机app?  制作网站的软件免费下载,免费制作app哪个平台好?  定制建站流程解析:需求评估与SEO优化功能开发指南  潍坊网站制作公司有哪些,潍坊哪家招聘网站好?  宝塔建站教程:一键部署配置流程与SEO优化实战指南  如何用好域名打造高点击率的自主建站?  网站制作多少钱一个,建一个论坛网站大约需要多少钱?  C++如何编写函数模板?(泛型编程入门)  简单实现Android文件上传  如何有效防御Web建站篡改攻击?  零服务器AI建站解决方案:快速部署与云端平台低成本实践  沈阳制作网站公司排名,沈阳装饰协会官方网站?  高端网站建设与定制开发一站式解决方案 中企动力  如何高效利用200m空间完成建站?  我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?  建站之星价格显示格式升级,你的预算足够吗?  如何设计高效校园网站?  如何快速搭建响应式可视化网站?  如何在Ubuntu系统下快速搭建WordPress个人网站?  建站主机助手选型指南:2025年热门推荐与高效部署技巧  如何在搬瓦工VPS快速搭建网站?  免费公司网站制作软件,如何申请免费主页空间做自己的网站?  如何用已有域名快速搭建网站?  如何选择高效响应式自助建站源码系统?  如何用y主机助手快速搭建网站?  小型网站制作HTML,*游戏网站怎么搭建?  弹幕视频网站制作教程下载,弹幕视频网站是什么意思?  如何规划企业建站流程的关键步骤?  在线制作视频的网站有哪些,电脑如何制作视频短片?  制作假网页,招聘网的薪资待遇,会有靠谱的吗?一面试又各种折扣?  如何批量查询域名的建站时间记录?  建站主机无法访问?如何排查域名与服务器问题  如何安全更换建站之星模板并保留数据?  建站VPS选购需注意哪些关键参数?  如何选择建站程序?包含哪些必备功能与类型?  css网站制作参考文献有哪些,易聊怎么注册?  深入理解Android中的xmlns:tools属性  如何通过商城自助建站源码实现零基础高效建站? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。