timer_create 函数返回 EINVAL

timer_create function returning EINVAL

我正在编写一个示例程序,其中我的 main() 将创建一个线程,然后它将启动一个计时器。当计时器到期时,线程应该得到信号。这是在 Ubuntu 18.04.4 LTS 上。

我的问题是 timer_create() 失败并且错误编号设置为 EINVAL。下面给出了 timer_create() 的代码片段。

    /* Create the timer */
    sevp.sigev_notify = SIGEV_THREAD_ID;
    sevp.sigev_signo = SIGALRM;
    sevp.sigev_value.sival_int = somevalue;
    sevp._sigev_un._tid = threadid;

    retval = timer_create(CLOCK_MONOTONIC,&sevp,&timerid);

    if ( 0 == retval )
    {
        printf("Success in creating timer [%p]",timerid);
    }
    else
    {
        printf("Error in creating timer [%s]\n",strerror(errno));
    }

我做错了什么?

根据 linux 手册页条目 timer_createSIGEV_THREAD_ID:

As for SIGEV_SIGNAL, but the signal is targeted at the thread whose ID is given in sigev_notify_thread_id, which must be a thread in the same process as the caller. The sigev_notify_thread_id field specifies a kernel thread ID, that is, the value returned by clone(2) or gettid(2). This flag is intended only for use by threading libraries.

线程ID(问题代码中的threadid)需要是内核线程ID。该 ID 可以通过 gettid.

获得