再谈pthread_cond_timedwait

今天有朋友咨询pthread_cond_timedwait的使用方法.为保证跨平台,线程库建议使用pthread,不论是windows还是linux都可以编译.
pthread win32主页
注意:虽然名字叫pthread win32,但是支持windows x64和windows x32
windows pthread下载
在这里插入图片描述
pthread_cond_timedwait还需要gettimeofday函数,但是windows上没有这个函数,需要自己定义:

#include <Windows.h>
#define FILETIME_UNITS_PER_SEC	10000000L
#define FILETIME_UNITS_PER_USEC 10
int gettimeofday(struct timeval* tp, struct timezone* tzp) {
	//https://docs.microsoft.com/zh-cn/windows/win32/api/minwinbase/ns-minwinbase-filetime?redirectedfrom=MSDN
	//FILETIME structure:Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
	// epoch =1970-01-01 -  1601-01-01 = 116444736000000000ULL; (why 1601?)
	static const unsigned __int64 epoch = 116444736000000000ULL;
	FILETIME	file_time;
	ULARGE_INTEGER ularge;

	GetSystemTimeAsFileTime(&file_time);
	ularge.LowPart = file_time.dwLowDateTime;
	ularge.HighPart = file_time.dwHighDateTime;

	tp->tv_sec = (long)((ularge.QuadPart - epoch) / FILETIME_UNITS_PER_SEC);
	tp->tv_usec = (long)(((ularge.QuadPart - epoch) % FILETIME_UNITS_PER_SEC)
		/ FILETIME_UNITS_PER_USEC);
	return 0;
}

重点

  • struct timeval中的tv_sec时间单位是秒, tv_usec的时间单位是微秒;
  • struct timezone在Windows上无用,windows内核时钟只精确至毫秒;
  • 1秒=1000毫秒=1000000微秒=1000000000纳秒

使用方法

struct timeval now;
struct timespec abstime;
int32_t ms = 100; //休眠100毫秒
gettimeofday(&now, NULL);
abstime.tv_sec = now.tv_sec;
//now.tv_usec * 1000微秒转换为纳秒
//(ms % 1000) * 1000000 毫秒转换为纳秒,其中毫秒的值不能超过1000
abstime.tv_nsec = now.tv_usec * 1000 + (ms % 1000) * 1000000;
pthread_mutex_lock(mutex);
pthread_cond_timedwait(cond, mutex,&abstime); --注意在休眠过程中锁已经释放,休眠结束后又重新锁定
pthread_mutex_unlock(mutex);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kmblack1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值