Question: I'm a newbie linux developer, so I don't know exactly how to solve a problem.

In my main I spawn many worker threads, each one executes its task and sleeps for a few seconds.

In some circumstances I have to terminate. I need to avoid to wait for each sleep termination, so I want to send a signal to each thread to interrupt the sleep (nanosleep in my case) and terminate the threadproc loop. 

I read a lot of documents about pthread\_kill, but when I send, for example a SIGUSR1 to a thread, the process terminates in an abnormal way.

Could somebody point me to the solution?

Thank you very much!!!

> 
> **Edit:** ***Solved***, the solution was `pthread_cond_timedwait` and `pthread_cond_signal`.
> 
> 
>

Answer: 
I don't know the details of your code but it sounds like a job for condition variables: [cs.nmsu.edu/~jcook/Tools/pthreads/pthread\_cond.html](http://www.cs.nmsu.edu/~jcook/Tools/pthreads/pthread_cond.html) – Galik

I've just implemented the solution based upon the `pthread_cond_signal` and `pthread_cond_timedwait` and I confirm that if works!!! – Dare Devil
In sending a signal, you're doing the right thing.
The only thing that is missing is catching that signal. You have to replace the signal action with your own one. In the signal handler you can set a variable and check for it after sleep returns.
Look into `sigaction`