首页 > 解决方案 > Two independent posix timers created by timer_create() and started with same value do not work as expected

问题描述

I created two independent timers one after another using timer_create(), and then started the timers using timer_settime() with same value, but the result is unpredictable.

...
struct sigevent sev1, sev2;
struct itimerspec its;
timer_t timerid1, timerid2;
...
sev1.sigev_notify = SIGEV_THREAD;
sev1.sigev_notify_function = hander1;
sev1.sigev_value.sigval_int = 999;

sev2.sigev_notify = SIGEV_THREAD;
sev2.sigev_notify_function = hander2;
sev2.sigev_value.sigval_int = 99999;

timer_create(CLOCK_REALTIME, &sev1, &timer_id1);
timer_create(CLOCK_REALTIME, &sev2, &timer_id2);

its.it_value.tv_sec = 1;
its.it_value.tv_nsev = 0;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsev = 0;

timer_settime(timer_id1, 0, &its);
timer_settime(timer_id2, 0, &its);
...

I expect that both handler1 and handler2 would excute after 1 seconds with no fixed order, but the fact is sometimes both excute as expected, sometimes only one of the handlers excutes, the excution result is unpredictable...

标签: linuxtimer

解决方案


推荐阅读