d**d 发帖数: 389 | 1 【 以下文字转载自 Programming 讨论区 】
发信人: dxxd (东邪西毒), 信区: Programming
标 题: 请教一个linux下的POSIX timer的问题。
发信站: BBS 未名空间站 (Fri May 13 17:06:15 2011, 美东)
我用linux下面的POSIX timer, timer_create(),timer_settime(),
为什么在调用了timer_settime()以后,立马就有一个time-out callback? 然后再每过
5秒后有一个time out?
难道不是我调用timer_settime()以后,timer开始计时, 等到5秒以后再出现第一
time out callback 吗?
非常感谢!
代码如下:
#include
#include
#include
#include
#include
void
handle (sigval_t v)
{
time_t t;
char p[32];
time (&t);
strftime(p,sizeof(p),"%T ",localtime(&t));
printf("%s thread 0x%x,val=%d,signal captured.\n",p,pthread_self(),
v.sival_int);
return;
}
timer_t tid1;
timer_t create_timer(int id)
{
struct sigevent se;
memset(&se,0,sizeof(se));
se.sigev_notify = SIGEV_THREAD;
se.sigev_notify_function = handle;
se.sigev_value.sival_int = id;
if(timer_create(CLOCK_REALTIME,&se,&tid1) < 0)
{
perror("timer_creat");
return -1;
}
printf( "timer_create successfully.\n");
return tid1;
}
int set_timer(timer_t id, long time)
{
struct itimerspec ts, ots;
ts.it_value.tv_sec =time;
ts.it_value.tv_nsec =0;
ts.it_interval.tv_sec=time;
ts.it_interval.tv_nsec=0;
if(timer_settime(id,TIMER_ABSTIME,&ts,&ots)<0)
{
perror( "timer_settime");
return -1;
}
}
int stop_timer(timer_t id)
{
set_timer(id,0);
}
int
main(void)
{
tid1=create_timer(1);
set_timer(tid1,5);
for(;;) | t*****d 发帖数: 23 | 2 I think someone already replied to your post:
if(timer_settime(id,TIMER_ABSTIME,&ts,&ots)<0)
^^^^^^^^^^^^ |
|