user5185042
user5185042

Reputation: 43

timer_create function returning EINVAL

I am writing a sample program where my main() will create a thread and then it will start a timer. When the timer expires, the thread should get the signal. This is on Ubuntu 18.04.4 LTS.

My problem is that timer_create() is failing and error number is set to EINVAL. My snippet of code for timer_create() is given below.

    /* 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));
    }

What am I doing wrong?

Upvotes: 1

Views: 332

Answers (1)

PiRocks
PiRocks

Reputation: 2016

As per the linux man page entry for timer_create with SIGEV_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.

The thread ID (threadid in the question code) needs to be a kernel thread ID. That ID can be obtained with gettid.

Upvotes: 1

Related Questions