当前位置: 技术问答>linux和unix
linux定时器
来源: 互联网 发布时间:2017-04-02
本文导语: 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 "...
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:%dn", 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:%dn", wday[p2->tm_wday],p2->tm_hour, p2->tm_min, p2->tm_sec);
p2 = NULL;
sleep(2);
while(1)
{
usleep(1000);
p2 = localtime(&timep1);
printf("%d",p2->tm_sec);
if(p2->tm_min ==12 )
printf("time arrived!");
}
}
int main()
{
pthread_t tid;
pthread_create(&tid,NULL,fun,NULL);
while(1);
return 0;
}
我本来是要用这个线程来模拟一个 每到某小时内的某一分钟时,去做某一件事情。
ps: 主要是这个定时器的定时时间周期 很长大约2个小时,我现在用一个线程来模拟的,(假如每到2:30.4:30 :6:30 。。。。这样的,然后发送一个消息去通知别人)
|
我完全没看懂你的代码在做什么,和你的描述有啥关系?
你的while里何时重新time获取过当前时间?
你的while里何时重新time获取过当前时间?