一、函数功能说明
pthread_cond_timedwait 等待一个条件变量,或者超时就会返回
POSIX有两种时钟类型
1、CLOCK_REALTIME: 系统范围内的实时时钟,是个软件时钟,可以通过命令等方式修改该系统时间.
2、CLOCK_MONOTONIC:系统起机时到现在的时间,不能被设置和修改.
pthread_cond_timedwait()在没有设置条件变量属性的时候,默认用的是CLOCK_REALTIME软件时间,
因此在极端情况下会出现实际等待的时间与设置的超时时间不同。
所以,对于linux的超时等待功能,最好是使用CLOCK_MONOTONIC进行实现,并且通过pthread_condattr_setclock实现。
而对于android系统而言,是不支持pthread_condattr_setclock,通过验证可以采用函数pthread_cond_timedwait_monotonic实现。
下面直接给出代码的实现功能。
二、超时等待功能
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/times.h>
#include <unistd.h>
#include <time.h>
static pthread_mutex_t s_mut = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t s_cond = PTHREAD_COND_INITIALIZER;
void PthreadAttr_Init(void);
unsigned long long getSysTime(void);
void waitTimeout(void);
void PthreadAttr_Init(void)
{
#if defined(ANDROID)
#else
pthread_condattr_t cattr;
int iRet = -1;
iRet = pthread_condattr_init(&cattr);
if (iRet != 0)
{
return;
}
pthread_mutex_init(&s_mut, NULL);
pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
pthread_cond_init(&s_cond, &cattr);
pthread_condattr_destroy(&cattr);
#endif
return;
}
void waitTimeout(void)
{
unsigned long long ullbefore = getSysTime();
unsigned long long ullafter = 0;
#if defined(ANDROID)
#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC) // 支持ANDROID下NDK的编译,采用相对时间
struct timespec outtime;
memset(&outtime, 0x00, sizeof(struct timespec ));
clock_gettime(CLOCK_MONOTONIC, &outtime);
outtime.tv_sec += 2;
pthread_mutex_lock(&s_mut);
pthread_cond_timedwait_monotonic(&s_cond,&s_mut, &outtime);
pthread_mutex_unlock(&s_mut);
ullafter = getSysTime();
printf("####01 interval[%lld] ms\n", ullafter - ullbefore);
#else //支持ANDROID下NDK的编译,采用绝对时间
struct timeval now;
struct itmespec outtime;
gettimeofday(&now, NULL);
outtime.tv_sec = now..tv_sec + 3;
outtime.tv_nsec = now.tv_usec * 1000;
pthread_mutex_lock(&s_mut);
pthread_cond_timedwait(&s_cond, &s_mut, &outtime);
pthread_mutex_unlock(&s_mut);
ullafter = getSysTime();
printf("####02 interval[%lld] ms\n", ullafter - ullbefore);
#endif
#else // 支持LINUX下的编译,采用绝对时间
struct timespec outtime;
memset(&outtime, 0x00, sizeof(struct timespec ));
clock_gettime(CLOCK_MONOTONIC, &outtime);
outtime.tv_sec += 4;
pthread_mutex_lock(&s_mut);
pthread_cond_timedwait(&s_cond, &s_mut, &outtime);
pthread_mutex_unlock(&s_mut);
ullafter = getSysTime();
printf("####03 interval[%lld] ms\n", ullafter - ullbefore);
#endif
return;
}
unsigned long long getSysTime(void)
{
unsigned long long milliseconds = 0;
struct tms t_tmsTime;
clock_t t_CurTime;
static int s_clks_per_sec = 0;
if (s_clks_per_sec == 0)
{
s_clks_per_sec = sysconf(_SC_CLK_TCK);
}
if (s_clks_per_sec == 0)
{
return 0;
}
t_CurTime = times(&t_tmsTime);
if (1000 % s_clks_per_sec == 0)
{
milliseconds = (1000 /s_clks_per_sec)*(unsigned long long )t_CurTime;//换算成毫秒
}
else
{
milliseconds = 1000 * (unsigned long long )t_CurTime/s_clks_per_sec;//换算成毫秒
}
return milliseconds;
}
int main(void)
{
PthreadAttr_Init();
waitTimeout();
return 0;
}
编译命令:
gcc test_ptthrad_conf_timewait_monotonic.c -o test_ptthrad_conf_timewait_monotonic -lpthread -lrt
linux下的测试结果:
####03 interval[4010] ms