Skip to content
Advertisement

Sending signal to pthread to abort sleeping

I’m have a pthread function, which sleeps most of the time using usleep() I would like to send a signal from the main process to the thread to interrupt the sleeping sometimes.

The problem is that I can’t find any reference of which signal exactly to send using pthread_kill()

the usleep() man page, states that SIGALRM should not be used together with usleep()

Do I need to use a specific signal, it doesn’t matter ?

Advertisement

Answer

usleep returns with EINTR on every signal that’s delivered to a thread/process. You’ll probably be best off using SIGUSR signals.

Also, usleep is now obsolete : consider using nanosleep instead. As an added bonus, you’ll know how much sleep time was remaining at the time of the delivery of the signal.

Advertisement