毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> linux >> 正文

linux定时器如何取消

更新时间:2013-5-31:  来源:毕业论文

linux定时器如何取消

#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;
}

定时器是全局的。一个进程只有一个定时器。

每次启动定时器的时候时间都要重置

问题: 当我不需要这个定时器的时候,怎么取消它呢?这里我不想结束线程来结束这个定时器,有么有其他的办法来解决?

将 struct itimerval itv 设为 0 就停止了

-。。。没那么难,再调用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.

使用 timer_create/timer_settime/timer_delete 也是可以的

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。