#include <stdio.h>
#include <stdlib.h>
#include<pthread.h>
#include<sys/time.h>
#include<unistd.h>
#include<signal.h>
int count = 0;
void set_timer()
{
struct itimerval itv, oldtv;
itv.it_interval.tv_sec = 7200;
itv.it_interval.tv_usec = 0;
itv.it_value.tv_sec = 1;
itv.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &itv, &oldtv);
}
void sigalrm_handler(int sig)
{
count++;
printf("timer signal.. %d\n", count);
time_t timep;
struct tm *p;
time(&timep);
p = localtime(&timep);
printf("%d:%d\n",p->tm_hour,p->tm_min);
}
void * fun()
{
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
timer_t timep,timep1;
struct tm *p,*p2;
time(&timep);
time(&timep1);
p=gmtime(&timep); //格林威治(GMT)时间
printf("%d%d%d ",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf("%s %d:%d:%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
p2=localtime(&timep); /*取得当地时间*/
printf ("%d%d%d ", (1900+p2->tm_year),( 1+p2->tm_mon), p2->tm_mday);
printf("%s %d:%d:%d\n", wday[p2->tm_wday],p2->tm_hour, p2->tm_min, p2->tm_sec);
signal(SIGALRM, sigalrm_handler);
set_timer();
}
int main()
{
pthread_t tid;
pthread_create(&tid,NULL,fun,NULL);
void *ret;
pthread_join(tid,&ret);
while(1);
return 0;
}
定时器是全局的。一个进程只有一个定时器。
每次启动定时器的时候时间都要重置
问题: 当我不需要这个定时器的时候,怎么取消它呢? 这里我不想结束线程来结束这个定时器,有么有其他的办法来解决?
7 个解决方案
#1
将 struct itimerval itv 设为 0 就停止了
#2
- -。。。没那么难,再调用setitimer设置一下就可以了。
struct itimerval {
struct timeval it_interval; /* next value */
struct timeval it_value; /* current value */
};
A timer which is set to
zero (it_value is zero or the timer expires and it_interval is zero) stops.
struct itimerval {
struct timeval it_interval; /* next value */
struct timeval it_value; /* current value */
};
A timer which is set to
zero (it_value is zero or the timer expires and it_interval is zero) stops.
#3
还是蛇叔,来得及时啊,呵呵,谢谢啊!
#4
是不是整个进程真的是共享这一个定时器,如果在其他线程再里用的话,会不会有影响呢?,呵呵,懂的就说一下啊1
#5
你的struct itimerval itv不是全局的
#6
使用 timer_create/timer_settime/timer_delete 也是可以的
#7
使用 timer_create/timer_settime/timer_delete 也是可以的
谢谢啊 收益匪浅啊,刚好工程需里需要频繁的用到定时!太感谢了
#1
将 struct itimerval itv 设为 0 就停止了
#2
- -。。。没那么难,再调用setitimer设置一下就可以了。
struct itimerval {
struct timeval it_interval; /* next value */
struct timeval it_value; /* current value */
};
A timer which is set to
zero (it_value is zero or the timer expires and it_interval is zero) stops.
struct itimerval {
struct timeval it_interval; /* next value */
struct timeval it_value; /* current value */
};
A timer which is set to
zero (it_value is zero or the timer expires and it_interval is zero) stops.
#3
- -。。。没那么难,再调用setitimer设置一下就可以了。
struct itimerval {
struct timeval it_interval; /* next value */
struct timeval it_value; /* current value */
};
A timer which is set to
zero (it_value is zero or the timer expires and it_interval is zero) stops.
还是蛇叔,来得及时啊,呵呵,谢谢啊!
#4
将 struct itimerval itv 设为 0 就停止了
#5
是不是整个进程真的是共享这一个定时器,如果在其他线程再里用的话,会不会有影响呢?,呵呵,懂的就说一下啊1
将 struct itimerval itv 设为 0 就停止了
#6
使用 timer_create/timer_settime/timer_delete 也是可以的
#7
使用 timer_create/timer_settime/timer_delete 也是可以的
谢谢啊 收益匪浅啊,刚好工程需里需要频繁的用到定时!太感谢了