linux - 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...
解决方案
推荐阅读
- php - 我们如何使用 php 使 sql 行中的特定值在 html 表行中显示其值,并在该表的下一行中显示其更新值?
- c++ - 用于多个实例的 C++ 提升随机数生成器集种子
- android - 将imageview转换为base64并存入mysql以及如何在Android中解码
- css - 背景图像在媒体查询中无法正确调整大小?
- delphi - Delphi:如何确定应用程序是否在 Win32 / Win64 下运行并在 64 位上自动启动 64 位版本?
- python - 交换单个链表的头尾
- java - 无法在 Lambda 函数 Java 8 中设置返回对象
- reactjs - 如何让我的反应代码更高效
- swift - 无法将“Int”类型的值转换为预期的参数类型“[CChar]”(又名“数组”
') - datatable - 使用 Firestore 和 Flutter 填充 DataTable(使用 StreamBuilder)