条件变量实现的定时器
用条件变量实现的一个简单的定时器,直接上代码吧
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
void * tick( void * arg){
pthread_cond_t cond;
pthread_mutex_t mutex;
pthread_mutex_init( &mutex,NULL);
pthread_cond_init( &cond,NULL);
struct timespec to;
int i = 0;
pthread_mutex_lock( &mutex);
to.tv_sec = time( NULL) + 1;
to.tv_nsec = 0;
while ( i < 5) {
int err = pthread_cond_timedwait( &cond, &mutex, &to);
if ( err == ETIMEDOUT){
printf( "time out %d: dispatch something...\n",i);
to.tv_sec = time( NULL) + 1;
to.tv_nsec = 0;
i++;
}
}
pthread_mutex_unlock( &mutex);
}
int main( )
{
pthread_t pid;
int i=0;
printf( "create thread.../n");
pthread_create( &pid,0,tick,0);
pthread_join( pid,NULL);
sleep( 1);
printf( "Succeed exit!/n");
}

浙公网安备 33010602011771号