POSIX linux间隔定时器的便携式解决方案(timer_create,timer_settime ...)

时间:2021-05-28 20:50:19

I have a piece of code that use linux POSIX interval timers (timer_create, timer_settime...) that I want to port it to Windows.

我有一段代码使用linux POSIX间隔计时器(timer_create,timer_settime ...),我想把它移植到Windows。

Is there a solution that will work on both platforms?

是否有适用于这两种平台的解决方案?

update:

timer_create will notify the caller by rising a signal or triggering a function when the timer expires

timer_create将通过上升信号或在定时器到期时触发功能来通知呼叫者

2 个解决方案

#1


0  

Through CygWin, POSIX functions generally work on both Linux and Windows.

通过CygWin,POSIX函数通常可以在Linux和Windows上运行。

For example, the following compiles and works in Cygwin well:

例如,以下编译并在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
}

#2


-2  

consider of using std::chrono features for c++11 http://www.cplusplus.com/reference/chrono/

考虑在c ++ 11中使用std :: chrono功能http://www.cplusplus.com/reference/chrono/

#1


0  

Through CygWin, POSIX functions generally work on both Linux and Windows.

通过CygWin,POSIX函数通常可以在Linux和Windows上运行。

For example, the following compiles and works in Cygwin well:

例如,以下编译并在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
}

#2


-2  

consider of using std::chrono features for c++11 http://www.cplusplus.com/reference/chrono/

考虑在c ++ 11中使用std :: chrono功能http://www.cplusplus.com/reference/chrono/