当前位置: 首页 > news >正文

Pthread定时锁与读写锁详解

目录

1.pthread_mutex_timedlock函数的优势

2.pthread_mutex_timedlock是如何避免无限期的阻塞

3.读写锁

4.读写锁的使用示例


1.pthread_mutex_timedlock函数的优势

  1. 可控的阻塞时长:可以指定一个超时时间,如果在该时间内未能获得锁,则返回ETIMEDOUT并退出阻塞。相比trylock手动实现超时,更简洁且原子性更强
  2. 平衡等待与响应性:既可以在一定时间内等待锁释放(适合需要最终获取锁的场景。即使可以用trylock非阻塞式的锁,可以使得程序继续运转,但若是我们要求该程序必须要获得锁上的资源,那么timedlock明显是更好的选择)。适合对实时性有要求的场景,确保线程在规定时间内要么获得锁,要么执行超时处理
  3. 简化超时逻辑:无需像trylock那样通过循环 + 睡眠实现超时,减少用户态代码复杂性,且底层实现更高效

    

pthread_mutex_timedlock函数基本等价于pthread_mutex_lock函数,但是如果超过了自己所设定的时间,pthread_mutex_timedlock函数不会锁定互斥量,但会返回错误码

返回值:成功则为0,失败则为错误码

struct timespec {time_t tv_sec;   // 秒数(自纪元以来的秒数)long   tv_nsec;  // 纳秒数(0-999,999,999)
};

2.pthread_mutex_timedlock是如何避免无限期的阻塞

#include <pthread.h>
#include <time.h>
#include <iostream>
#include <stdlib.h>
#include <cstring>
int main()
{int err;struct timespec tout;struct tm *tmp;char buf[64];pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;pthread_mutex_lock(&lock);std::cout << "mutex is lock" << std::endl;clock_gettime(CLOCK_REALTIME, &tout);tmp = localtime(&tout.tv_sec);strftime(buf, sizeof(buf), "%r", tmp);std::cout << "current time is " << buf << std::endl;tout.tv_sec += 10; // 10秒以后err = pthread_mutex_timedlock(&lock, &tout);clock_gettime(CLOCK_REALTIME, &tout);tmp = localtime(&tout.tv_sec);strftime(buf, sizeof(buf), "%r", tmp);std::cout << "current time is " << buf << std::endl;if(err == 0)std::cout<<"mutex locked again!"<<std::cout;elsestd::cout<<"can't lock mutex again :"<<strerror(err);return 0;
}

3.读写锁

读写锁类似于互斥量,不同之处在于它允许更高的并行性。读写锁可能有三种状态:读模式锁定、写模式锁定和未锁定。一次只能有一个线程在写模式下持有读写锁,但多个线程可以同时在读模式下持有读写锁。

虽然各种实现对读写锁的实现方式有所不同,但如果锁已在读模式下被持有,并且一个线程在试图以写模式获取该锁时被阻塞,则读写锁通常会阻止其他读者。这可以防止源源不断的读者让等待的写者感到饥饿。

适用环境:读写锁非常适合对数据结构的读取频率高于修改频率的情况

要以读模式锁定读写锁,需要调用pthread_rwlock_rdlock函数。要以写模式锁定读写锁,则需要调用pthread_rwlock_wrlock函数。无论如何锁定一个读写锁,都可以通过调用pthread_rwlock_unlock函数来解锁它

4.读写锁的使用示例

#include <pthread.h>
#include <time.h>
#include <iostream>
#include <stdlib.h>
#include <cstring>
int main()
{int err;struct timespec tout;struct tm *tmp;char buf[64];pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;pthread_mutex_lock(&lock);std::cout << "mutex is lock" << std::endl;clock_gettime(CLOCK_REALTIME, &tout);tmp = localtime(&tout.tv_sec);strftime(buf, sizeof(buf), "%r", tmp);std::cout << "current time is " << buf << std::endl;tout.tv_sec += 10; // 10秒以后err = pthread_mutex_timedlock(&lock, &tout);clock_gettime(CLOCK_REALTIME, &tout);tmp = localtime(&tout.tv_sec);strftime(buf, sizeof(buf), "%r", tmp);std::cout << "current time is " << buf << std::endl;if(err == 0)std::cout<<"mutex locked again!"<<std::cout;elsestd::cout<<"can't lock mutex again :"<<strerror(err);return 0;
}


文章转载自:

http://5GxPvyXi.ydxwj.cn
http://pp6LF6nM.ydxwj.cn
http://QUQB5qtA.ydxwj.cn
http://EX8FIE58.ydxwj.cn
http://EbAWWsC9.ydxwj.cn
http://b29XLIOM.ydxwj.cn
http://SHSspwYV.ydxwj.cn
http://iNuSUZv5.ydxwj.cn
http://Gmqu7Ufo.ydxwj.cn
http://KXvExgS3.ydxwj.cn
http://RxFMQWOO.ydxwj.cn
http://be4w7njk.ydxwj.cn
http://bZ4L6JMx.ydxwj.cn
http://aWZTYZpo.ydxwj.cn
http://PmO6ovMS.ydxwj.cn
http://PlKbPgxC.ydxwj.cn
http://FTcFXkIo.ydxwj.cn
http://nJOXd9xs.ydxwj.cn
http://SIV6YwVp.ydxwj.cn
http://2lyIebBG.ydxwj.cn
http://Ga8n2b70.ydxwj.cn
http://6pfL72G1.ydxwj.cn
http://o2BEinoa.ydxwj.cn
http://h6OsPpbO.ydxwj.cn
http://RNTyoIvT.ydxwj.cn
http://zkGl6ic8.ydxwj.cn
http://ormSvjMY.ydxwj.cn
http://vdxZMrqt.ydxwj.cn
http://oOU0iCRQ.ydxwj.cn
http://GkpnMAG6.ydxwj.cn
http://www.dtcms.com/a/377518.html

相关文章:

  • Go模块自动导入教学文档
  • 技术文章大纲:开学季干货——知识梳理与经验分享
  • TensorFlow平台介绍
  • Vue3 中实现按钮级权限控制的最佳实践:从指令到组件的完整方案
  • 生成模型与概率分布基础
  • Cookie之domain
  • JavaSSM框架-MyBatis 框架(五)
  • 中州养老:设备管理介绍
  • 【Day 51|52 】Linux-tomcat
  • MySQL - 如果没有事务还要锁吗?
  • “高德点评”上线,阿里再战本地生活
  • JUC的常见类、多线程环境使用集合类
  • 《C++ 108好库》之1 chrono时间库和ctime库
  • C++篇(7)string类的模拟实现
  • 弱加密危害与修复方案详解
  • 【Linux】Linux常用指令合集
  • Android- Surface, SurfaceView, TextureView, SurfaceTexture 原理图解
  • 如何设计Agent 架构
  • MySQL主从不一致?DBA急救手册:14种高频坑点+3分钟定位+无损修复!
  • 拍我AI:PixVerse国内版,爱诗科技推出的AI视频生成平台
  • 3D柱状图--自定义柱子颜色与legend一致(Vue3)
  • LeetCode热题100--199. 二叉树的右视图--中等
  • Next系统学习(三)
  • Python深度学习:NumPy数组库
  • Django时区感知
  • PostgreSQL15——Java访问PostgreSQL
  • Shell 函数详解
  • 【系统分析师】第21章-论文:系统分析师论文写作要点(核心总结)
  • Linux 命令(top/ps/netstat/vmstat/grep/sed/awk)及服务管理(systemd)
  • 【图像生成】提示词技巧