Using Sleep() while using timers through setitimer
From the alarm(2)
man page:
sleep()
may be implemented usingSIGALRM
; mixing calls toalarm()
andsleep()
is a bad idea.
Some implementations don't use SIGALRM
, find a machine like that and you're set. Otherwise, you can try nanosleep()
; it's implemented safely. From the 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.
I'd use a library that gives an abstraction to these OS services. I use ACE library for timers and sleeps (ACE_OS::sleep
, ACE_Reactor::schedule_timeout
) and they work together without any problems. As far as I know ACE uses select
for its timers. I guess boost::thread::sleep and boost::asio::deadline_timer will accomplish the task successfully as well.