Linux多线程锁属性设置方法

发布时间 - 2026-01-10 22:19:13    点击率:

互斥锁是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 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel如何获取当前用户信息_Laravel Auth门面获取用户ID  如何快速搭建高效WAP手机网站?  Laravel如何正确地在控制器和模型之间分配逻辑_Laravel代码职责分离与架构建议  夸克浏览器网页跳转延迟怎么办 夸克浏览器跳转优化  手机钓鱼网站怎么制作视频,怎样拦截钓鱼网站。怎么办?  Laravel如何配置Horizon来管理队列?(安装和使用)  Laravel如何自定义错误页面(404, 500)?(代码示例)  高性价比服务器租赁——企业级配置与24小时运维服务  如何用AI帮你把自己的生活经历写成一个有趣的故事?  Laravel如何使用查询构建器?(Query Builder高级用法)  如何快速搭建高效服务器建站系统?  香港服务器WordPress建站指南:SEO优化与高效部署策略  如何快速生成ASP一键建站模板并优化安全性?  HTML 中如何正确使用模板变量为元素的 name 属性赋值  如何用AWS免费套餐快速搭建高效网站?  Laravel的.env文件有什么用_Laravel环境变量配置与管理详解  悟空识字怎么关闭自动续费_悟空识字取消会员自动扣费步骤  EditPlus中的正则表达式 实战(1)  ChatGPT 4.0官网入口地址 ChatGPT在线体验官网  宙斯浏览器文件分类查看教程 快速筛选视频文档与图片方法  如何快速搭建高效香港服务器网站?  Python制作简易注册登录系统  网站制作壁纸教程视频,电脑壁纸网站?  如何基于云服务器快速搭建个人网站?  如何在IIS中新建站点并解决端口绑定冲突?  JavaScript如何实现错误处理_try...catch如何捕获异常?  Laravel怎么实现搜索高亮功能_Laravel结合Scout与Algolia全文检索【实战】  高防服务器:AI智能防御DDoS攻击与数据安全保障  Swift中switch语句区间和元组模式匹配  韩国服务器如何优化跨境访问实现高效连接?  HTML5打空格有哪些误区_新手常犯的空格使用错误【技巧】  ai格式如何转html_将AI设计稿转换为HTML页面流程【页面】  如何用PHP快速搭建CMS系统?  Edge浏览器提示“由你的组织管理”怎么解决_去除浏览器托管提示【修复】  香港服务器网站卡顿?如何解决网络延迟与负载问题?  网站优化排名时,需要考虑哪些问题呢?  Laravel如何使用Blade模板引擎?(完整语法和示例)  如何快速选择适合个人网站的云服务器配置?  如何快速生成可下载的建站源码工具?  Google浏览器为什么这么卡 Google浏览器提速优化设置步骤【方法】  北京网页设计制作网站有哪些,继续教育自动播放怎么设置?  瓜子二手车官方网站在线入口 瓜子二手车网页版官网通道入口  韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南  深圳防火门网站制作公司,深圳中天明防火门怎么编码?  焦点电影公司作品,电影焦点结局是什么?  郑州企业网站制作公司,郑州招聘网站有哪些?  Linux后台任务运行方法_nohup与&使用技巧【技巧】  三星、SK海力士获美批准:可向中国出口芯片制造设备  如何打造高效商业网站?建站目的决定转化率  如何用狗爹虚拟主机快速搭建网站?