博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pthread_mutex_timedlock
阅读量:7218 次
发布时间:2019-06-29

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

函数pthread_mutex_timedlock当线程试图获取一个已加锁的互斥变量时,pthread_mutex_timedlock互斥量原语允许绑定线程阻塞的时间。pthread_mutex_timedlock函数与pthread_mutex_lock是基本等价的,但是在达到超时时间值时,pthread_mutex_timedlock不会对互斥量进行加锁,而是返回错误码ETIMEDOUT。#include
#include
int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex,const struct timespec *restrict tsptr);超时指定愿意等待的绝对时间(与相对时间对比而言,指定在时间x之前可以阻塞等待,而不是说愿意阻塞Y秒)。这个超时时间是用timespec结构来表示的,它用秒和纳秒来描述时间。

 

#include"apue.h"#include 
#include
intmain(void){ int err; struct timespec tout; struct tm *tmp; char buf[64]; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&lock); printf("mutex is locked\n"); clock_gettime(CLOCK_REALTIME, &tout); tmp = localtime(&tout.tv_sec); strftime(buf, sizeof(buf), "%r", tmp); printf("current time is %s\n", buf); pthread_mutex_unlock(&lock);//这个是我自己加的,为了测试这个程序 tout.tv_sec += 10; /* 10 seconds from now */ /* caution: this could lead to deadlock */ err = pthread_mutex_timedlock(&lock, &tout); clock_gettime(CLOCK_REALTIME, &tout); tmp = localtime(&tout.tv_sec); strftime(buf, sizeof(buf), "%r", tmp); printf("the time is now %s\n", buf); if (err == 0) printf("mutex locked again!\n"); else printf("can't lock mutex again: %s\n", strerror(err)); exit(0);}

转载地址:http://jotym.baihongyu.com/

你可能感兴趣的文章
批处理学习笔记
查看>>
Linux挂载磁盘
查看>>
Cyclone II RAM ROM设置
查看>>
Ubuntu下实现伪静态
查看>>
python 二维数组遍历
查看>>
第8周课下作业1(补)
查看>>
阿萨斯
查看>>
service启动和停止,绑定和解除绑定
查看>>
elasticsearch开机启动脚本
查看>>
window service 恢复选项卡设置
查看>>
车辆管理系统之编码过程总结(十一)
查看>>
基于AOE网的关键路径的求解
查看>>
2017-5-16 python标准库
查看>>
浅谈游戏的声音处理-流播放文件 source
查看>>
旧版本转换成支持ARC版本
查看>>
创建与服务器的输入输出流
查看>>
string.hのmemmove的实现
查看>>
dicom网络通讯入门(1)
查看>>
日常训练.jpg
查看>>
iOS----KVC和KVO 详解
查看>>