#include <stdio.h>
#include <time.h>
#define NET_IDEL_INTERVAL_SEC 3
#define NET_IDEL_INTERVAL_NSEC 0
pthread_mutex_t net_call_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t net_call_cond = PTHREAD_COND_INITIALIZER;
void wait_net_call(void)
{
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
tv.tv_sec += NET_IDEL_INTERVAL_SEC;
tv.tv_nsec += NET_IDEL_INTERVAL_NSEC;
pthread_mutex_lock(&net_call_mutex);
pthread_cond_timedwait(&net_call_cond, &net_call_mutex, &tv);
pthread_mutex_unlock(&net_call_mutex);
}
void signal_net_call(void)
{
/* DBG("Send a net call!\n"); */
pthread_mutex_lock(&net_call_mutex);
pthread_cond_signal(&net_call_cond);
pthread_mutex_unlock(&net_call_mutex);
}
void aa()
{
for(;;)
{
printf("111111111111111111111111111\n");
wait_net_call();
}
}
int main()
{
pthread_t id1;
int ret;
ret = pthread_create(&id1, NULL, (void *)aa, (void *)1);
if (0 != ret)
{
printf("thread 1 create failed!\n");
return 1;
}
for(;;)
{
sleep(1);
printf("send-net-call\n");
signal_net_call();
}
}
signal_net_call() 为停止此次定时器函数;调用会运行pthread_cond_timedwait下面的函数。