c - POSIX Linux 间隔定时器的可移植解决方案(timer_create、timer_settime...)

标签 c linux windows timer signals

我有一段使用 Linux POSIX 间隔计时器(timer_create、timer_settime...)的代码,我想将其移植到 Windows。

是否有一个适用于两个平台的解决方案?

更新:

timer_create将在定时器到期时通过上升信号或触发函数来通知调用者

最佳答案

通过 CygWin,POSIX 函数通常可以工作 Linux 和 Windows 均适用。

例如,以下代码在 Cygwin 中编译并运行良好:

#include <signal.h>
#include <unistd.h>
#include <time.h>
int main()
{
    timer_t tmr;
    //error checking omitted for brevity
    timer_create(CLOCK_REALTIME, &(struct sigevent){ .sigev_notify=SIGEV_SIGNAL, .sigev_signo=SIGALRM}, &tmr);
    timer_settime(tmr, 0, &(struct itimerspec){ .it_value={.tv_sec=3}}, 0);
    pause(); //will break after 3s
}

关于c - POSIX Linux 间隔定时器的可移植解决方案(timer_create、timer_settime...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51656515/

相关文章:

linux - Bash PATH 长度限制

c# - 错误 1001 : The Specified Service Already Exists. 无法删除现有服务

c - Frama-C:在 Cygwin/Windows 8.1 上编译

c - 'char *' 和 'char (*) [100]' 有什么区别?

c - 使用 strcmp 的段错误

c - 如何在 C 中分离缓冲区结果

c# - 在 Linux 中使用 Mono 识别可执行文件?

C 的 pow 函数拒绝使用可变指数

c# - 如何使用 C# 中的关联应用程序执行多个 ".ext"

c - 与 Windows 相比,为什么 Screen 在 Linux 中没有消失?