简体   繁体   中英

timer_create and timerid, is this allowed?

I can't set the evp parameter to NULL , but I'd like to receive the timerid in my timer handler, just as if it had been set to NULL . I was thinking about calling:

struct sigevent se;
se.sigev_notify = SIGEV_THREAD;
se.sigev_notify_attributes = {};
se.sigev_notify_function = timer_handler;

timer_create(CLOCK_MONOTONIC, &se, &se.sigev_value);

I am not certain, whether I should do this, even if it worked. Is there another way to obtain the timerid in the timer handler, without setting evp to NULL ?

That is definitely invalid because timer_t could be longer than an int . Also passing a pointer to a union is not the same as passing a pointer to one of its members.

You need to do something like this:

timer_t timerid;
struct sigevent se = { 0 };
se.sigev_notify = SIGEV_THREAD;
se.sigev_value.sival_ptr = &timerid;
se.sigev_notify_function = timer_handler;

if (timer_create(CLOCK_MONOTONIC, &se, &timerid) < 0)
    abort(); // or whatever

// later...
if (timer_delete(timerid) < 0)
    abort();

In other words, you need to allocate space to hold the timerid , squirrel away a pointer to it in your sigevent structure, and remember to delete the timer when you are done with it.

Also you should always check system calls for errors and do something other than proceeding like nothing happened.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM