我通过 sys/time.h 中的 setitimer 函数在我的 C++ 代码中使用了一个计时器。这会将 SIGALRM 信号映射到我的计时器处理程序方法。在此之后我无法使用 sleep 功能。我假设这是因为 sleep 也使用 SIGALRM 信号。您可以针对此问题提出任何解决方法吗?
感谢您的回复。
最佳答案
来自 alarm(2)
man page :
sleep()
may be implemented usingSIGALRM
; mixing calls toalarm()
andsleep()
is a bad idea.
有些实现不使用 SIGALRM
,找一台这样的机器就可以了。否则,您可以尝试 nanosleep()
;它已安全实现。来自 nanosleep(2)
man page :
Compared to
sleep(3)
andusleep(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/