因此,直到最近,当我们决定将其移至 Lubuntu 12.04 系统时,这段代码才能正常工作。对 timer_settime 的调用返回 EINVAL,并在 gdb 下运行它我已经确认 ts 的所有字段在调用时都在 0 和 999999999 之间:
1067 if(-1 ==timer_settime(tid,0,&ts,NULL))
(gdb) print ts
$1 = {it_interval = {tv_sec = 0, tv_nsec = 200000000}, it_value = {tv_sec = 0,
tv_nsec = 0}}
因为这应该是唯一可以导致它返回 EINVAL 的东西,所以我非常困惑。也许这里有一些明显的东西我错过了。
struct sigevent sev;
struct itimerspec ts;
timer_t *tid;
//actually point the pointer at something.
tid = calloc(1,sizeof(timer_t));
//make sure there's no garbage in the structures.
memset(&sev,0,sizeof(struct sigevent));
memset(&ts,0, sizeof(struct itimerspec));
//notify via thread
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = SwitchThreadHandler;
sev.sigev_notify_attributes = NULL;
sev.sigev_value.sival_ptr = tid;
ts.it_value.tv_sec =0;
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 200000000;
if(-1 == timer_create(CLOCK_REALTIME,&sev,tid))
{
retval = EX_SOFTWARE;
fprintf(stderr,"Failed to create timer.");
free(tid);
return retval;
}
if(-1 ==timer_settime(tid,0,&ts,NULL))
{
int errsv = errno;
fprintf(stderr,"timer_settime FAILED!!!\n");
if(errsv == EINVAL)
{
fprintf(stderr,"INVALID VALUE!\n");
}
else
{
fprintf(stderr,"UNKOWN ERROR: %d\n",errsv);
}
return EX_SOFTWARE;
}