即使设置了itimerspec的所有成员,计时器\u settime也会失败

即使设置了itimerspec的所有成员,计时器\u settime也会失败,c,timer,posix,errno,C,Timer,Posix,Errno,所以,这段代码一直运行良好,直到最近,我们决定将其移动到Lubuntu 12.04系统。对timer_settime的调用返回EINVAL,在gdb下运行它时,我已确认ts的所有字段在调用时都在0和99999999之间: 1067 if(-1 ==timer_settime(tid,0,&ts,NULL)) (gdb) print ts $1 = {it_interval = {tv_sec = 0, tv_nsec = 200000000}, it_value = {tv

所以,这段代码一直运行良好,直到最近,我们决定将其移动到Lubuntu 12.04系统。对timer_settime的调用返回EINVAL,在gdb下运行它时,我已确认ts的所有字段在调用时都在0和99999999之间:

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}}
因为这应该是唯一能让它返回艾因瓦尔的东西,所以我非常困惑。也许这里有一些明显的东西我遗漏了

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

timer\u settime
被记录为将
timer\u t
作为第一个参数,而不是
timer\u t*
类似于
timer\u create
。如果
timerid
无效,它将使用
EINVAL
失败

因此,您应该将
*tid
作为第一个参数传递


请注意,您的编译器应该对此发出警告。

奇怪的是,gcc-Wall没有对此给出任何提示,而且它在我们已经尝试过的四种环境中有三种环境下工作得非常好。奇怪的好吧,现在它不再失败了,但计时器从不启动,所以你已经回答了我问的问题。