1. struct timeval tv;
tv.tv_usec = 0;
tv.tv_sec = 1;
select(1,NULL,NULL,NULL,&tv);
使线程睡眠1秒.
2.
void sleepthread(int sectime,int nsectime)
{
pthread_cond_t mycond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
struct timespec ts;
int rv;
//1 second = 1000 ms 1ms = 1000 us 1us = 1000ns
struct timeval now;
gettimeofday(&now, NULL);
ts.tv_sec = now.tv_sec + sectime ;
ts.tv_nsec = now.tv_usec * 1000 + nsectime;
pthread_mutex_lock(&mymutex);
rv = pthread_cond_timedwait(&mycond, &mymutex, &ts);
switch(rv)
{
case ETIMEDOUT:
printf("timeout occur \n");
break;
case EINTR:
printf("interrupt occur \n");
default:
/* Handle errors */
case 0:
break;
}
pthread_mutex_unlock(&mymutex);
}
|
|
|
|
|
阅读(969) | 评论(0) | 转发(0) |