Advanced Programming in UNIX Environment Episode 54
发布日期:2021-10-07 23:47:35 浏览次数:1 分类:技术文章

本文共 2281 字,大约阅读时间需要 7 分钟。

pthread_mutex_timedlock Function

One additional mutex primitive allows us to bound the time that a thread blocks when a mutex it is trying to acquire is already locked. The pthread_mutex_timedlock function is equivalent to pthread_mutex_lock, but if the timeout value is reached, pthread_mutex_timedlock will return the error code ETIMEDOUT without locking the mutex.

#include 
#include
int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex, const struct timespec *restrict tsptr);

The timeout specifies how long we are willing to wait in terms of absolute time (as opposed to relative time; we specify that we are willing to block until time X instead of saying that we are willing to block for Y seconds). The timeout is represented by the timespec structure, which describes time in terms of seconds and nanoseconds.

 

Using pthread_mutex_timedlock

Reader–Writer Locks

Reader–writer locks are similar to mutexes, except that they allow for higher degrees of parallelism. With a mutex, the state is either locked or unlocked, and only one thread can lock it at a time. Three states are possible with a reader–writer lock: locked in read mode, locked in write mode, and unlocked. Only one thread at a time can hold a reader–writer lock in write mode, but multiple threads can hold a reader–writer lock in read mode at the same time.

As with mutexes, reader–writer locks must be initialized before use and destroyed before freeing their underlying memory.

#include 
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, cosnt pthread_rwlockattr_t *restrict attr);int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

The Single UNIX Specification defines the PTHREAD_RWLOCK_INITIALIZER constant in the XSI option. It can be used to initialize a statically allocated reader–writer lock when the default attributes are sufficient.

To lock a reader–writer lock in read mode, we call pthread_rwlock_rdlock. To write lock a reader–writer lock, we call pthread_rwlock_wrlock. Regardless of how we lock a reader–writer lock, we can unlock it by calling pthread_rwlock_unlock.

#include 
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

转载地址:https://blog.csdn.net/myfather103/article/details/79824217 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Advanced Programming in UNIX Environment Episode 55
下一篇:Advanced Programming in UNIX Environment Episode 53

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年03月09日 05时45分00秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

斥候密报_斥候密报《最强王者》三国幕后巾帼之黄月英_吉吉建站手游网 2019-04-21
python的循环控制结构是什么_7.Python控制和循环结构 2019-04-21
python 死循环插曲变量_FishC03 讲:python小插曲之变量和字符串 2019-04-21
车型代号对照表_车型代号对照表_相关文章专题_写写帮文库 2019-04-21
arcgis符号方向_ArcGIS制图表达-河流渐变与符号旋转 2019-04-21
springboot 实现机器学习_SpringBoot架构浅谈 2019-04-21
oss批量上传工具_OssExplorer一OSS的专用客户端工具【最新版】_Windows_Windows server 2008-云市场-阿里云... 2019-04-21
login控件authenticate_ASP:Login控件(登录控件) 2019-04-21
drf 安装_drf 安装与配置 2019-04-21
c++ loadlibrary 初始化对象_C++构造函数和初始化表 2019-04-21
jmeter mysql driver_jmeter测试mysql数据库之JDBC请求 2019-04-21
mysql group by cube_SQL Server 之 GROUP BY、GROUPING SETS、ROLLUP、CUBE 2019-04-21
mysql mgr 5.6_mysql MGR高可用配置 2019-04-21
lua 区间比较_TI-Lua 系列教程2.4.1: 条件分支 2019-04-21
mysql同时多表插入_MySQL多表同时插入 2019-04-21
postman delete 请求传递数组_Postman请求方法 2019-04-21
基于mysql学生签到_Java swing mysql学生签到考勤系统附带完整源码及开发视频 2019-04-21
go mysql 多并发_MySQL并发处理-Go语言中文社区 2019-04-21
mysql定义变量字符串类型_mysqli_stmt :: bind_param():类型定义字符串中的元素数量与绑定变量的数量不匹配... 2019-04-21
mysql测试数据100w_利用MySQL存储过程批量插入100W条测试数据 2019-04-21