Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Sleep() while using timers through setitimer

I am using a timer in my C++ code through setitimer function from sys/time.h. This maps the SIGALRM signal to my timer handler method. After this I am not able to use sleep function. I am assuming it is because sleep uses SIGALRM signal as well. Can you suggest any workaround for this problem?

Thanks for replying.

like image 251
SkypeMeSM Avatar asked Nov 21 '25 16:11

SkypeMeSM


1 Answers

From the alarm(2) man page:

sleep() may be implemented using SIGALRM; mixing calls to alarm() and sleep() 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) 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.

like image 125
Carl Norum Avatar answered Nov 23 '25 06:11

Carl Norum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!