----------------------- linux -----------------------
//初始化
pthread_mutex_t mutex_lock;
pthread_cond_t handle:
pthread_cond_init(&handle, NULL);
//等待事件触发
struct timeval now_time;
gettimeofday(&now_time, NULL);
struct timespec abstime;
struct timeval now;
gettimeofday(&now, NULL);
unsigned int nsec = now.tv_usec * 1000 + (time_out % 1000) * 1000000;
abstime.tv_nsec = nsec % 1000000000;
abstime.tv_sec = now.tv_sec + nsec/1000000000 + time_out/1000;
int iRet = pthread_cond_timedwait(&handle, &mutex_lock, &abstime);
if(iRet != 0)
{
printf("Time Out !!!\n");
return 0 ;
}
//通知,事件触发
pthread_cond_signal(&handle);
//释放
pthread_cond_destroy(&handle);
//初始化
HANDLE handle;
handle = CreateEvent(NULL,FALSE,FALSE,NULL);
if(handle == NULL)
{
printf("CreateEvent failed (%d)\n", GetLastError());
}
//等待事件触发
int time_out = 1000;
DWORD iRet = WaitForSingleObject(handle, time_out);
if(iRet == WAIT_TIMEOUT || wait_obj == iRet)
{
//超时
return 0;
}
//通知,事件触发
if(!SetEvent(handle))
{
printf("SetEvent failed (%d)\n", GetLastError());
return -1;
}
//释放
if(handle != NULL)
{
CloseHandle(handle);
//初始化
pthread_mutex_t mutex_lock;
pthread_cond_t handle:
pthread_cond_init(&handle, NULL);
//等待事件触发
struct timeval now_time;
gettimeofday(&now_time, NULL);
struct timespec abstime;
struct timeval now;
gettimeofday(&now, NULL);
unsigned int nsec = now.tv_usec * 1000 + (time_out % 1000) * 1000000;
abstime.tv_nsec = nsec % 1000000000;
abstime.tv_sec = now.tv_sec + nsec/1000000000 + time_out/1000;
int iRet = pthread_cond_timedwait(&handle, &mutex_lock, &abstime);
if(iRet != 0)
{
printf("Time Out !!!\n");
return 0 ;
}
//通知,事件触发
pthread_cond_signal(&handle);
//释放
pthread_cond_destroy(&handle);
//初始化
HANDLE handle;
handle = CreateEvent(NULL,FALSE,FALSE,NULL);
if(handle == NULL)
{
printf("CreateEvent failed (%d)\n", GetLastError());
}
//等待事件触发
int time_out = 1000;
DWORD iRet = WaitForSingleObject(handle, time_out);
if(iRet == WAIT_TIMEOUT || wait_obj == iRet)
{
//超时
return 0;
}
//通知,事件触发
if(!SetEvent(handle))
{
printf("SetEvent failed (%d)\n", GetLastError());
return -1;
}
//释放
if(handle != NULL)
{
CloseHandle(handle);
}