我有一段使用linux POSIX间隔计时器(timer_create,timer_settime...)的代码。我想把它移植到Windows上。
有没有在两个平台上都能工作的解决方案?
更新:
当计时器超时时,timer_create将通过发出信号或触发函数来通知调用者
发布于 2018-08-02 23:06:46
通过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
}发布于 2018-08-02 22:58:09
考虑在c++11 http://www.cplusplus.com/reference/chrono/中使用std::chrono功能
https://stackoverflow.com/questions/51656515
复制相似问题