c++ - 通过 setitimer 使用定时器时使用 Sleep()

标签 c++ unix timer sleep

我通过 sys/time.h 中的 setitimer 函数在我的 C++ 代码中使用了一个计时器。这会将 SIGALRM 信号映射到我的计时器处理程序方法。在此之后我无法使用 sleep 功能。我假设这是因为 sleep 也使用 SIGALRM 信号。您可以针对此问题提出任何解决方法吗?

感谢您的回复。

最佳答案

来自 alarm(2) man page :

sleep() may be implemented using SIGALRM; mixing calls to alarm() and sleep() is a bad idea.

有些实现不使用 SIGALRM,找一台这样的机器就可以了。否则,您可以尝试 nanosleep();它已安全实现。来自 nanosleep(2) man page :

Compared to sleep(3) and usleep(3), nanosleep() has the advantage of not affecting any signals, it is standardized by POSIX, it provides higher timing resolution, and it allows to continue a sleep that has been interrupted by a signal more easily.

关于c++ - 通过 setitimer 使用定时器时使用 Sleep(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3645026/

相关文章:

c++ - 在需要 sudo 运行的 NetBeans 8.0 中调试 C++ 程序

c++ - GLM 功能不起作用

c++ - 模板声明和定义的含义

c++ - 为 gettext boost 语言环境 "Conversion failed"

python - 通过crontab安排python脚本

unix - write()和printf()之间的区别

linux - Unix管道到ls

java - 有人可以解释为什么我使用 Timer 时会出现此错误吗?

java - CountDownTimer.cancel() 在 Android 中不起作用

c++ - 如何同步两个程序的定时器