Timer freezes after some time
<p>I am running the following program which implements a timer. When a thread awake after receiving a signal on condition variable from the previous running thread, it creates a timer and send a signal to the next thread on timer expiration. I want it to run for some time, but the timer stops ticking after some runs.</p>

<pre><code>//Import 
#define _POSIX_C_SOURCE 199309
#include &lt;sched.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/wait.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;pthread.h&gt;
#include &lt;unistd.h&gt;
#include &lt;time.h&gt;
#include &lt;sys/time.h&gt;
#include &lt;signal.h&gt;
#include &lt;errno.h&gt;
#define NUM_THREADS 10

#define CLOCKID CLOCK_REALTIME
#define SIG SIGUSR1
timer_t timerid;

pthread_cond_t   condA[NUM_THREADS+1]  = PTHREAD_COND_INITIALIZER;
pthread_mutex_t  mutex  = PTHREAD_MUTEX_INITIALIZER;

pthread_t tid[NUM_THREADS];
int state = 0;

static void handler(int sig, siginfo_t *si, void *uc)
{
    if(si-&gt;si_value.sival_ptr != &amp;timerid){
        printf("Stray signal\n");
    } else {
        //printf("Caught signal %d from timer\n", sig);
    }

        pthread_cond_signal(&amp;condA[state]);
}

void *threadA(void *data_)
{
    int i = 0, s;
    long int loopNum, j;

    int turn = (intptr_t)data_;

    struct timeval tval_result;

    // Timer's part starts

    struct sigevent sev;
    struct itimerspec its;
    long long freq_nanosecs;
    sigset_t mask;
    struct sigaction sa;
    // TImer'spart ends

    while(1)
    {

        /* Wait for state A */
        pthread_mutex_lock(&amp;mutex);

        for (;state != turn;)
        {
            s = pthread_cond_wait(&amp;condA[turn], &amp;mutex);
            if (s != 0)
                perror("pthread_cond_wait");
           // printf("main(): state = %d\n", state);
        }
        pthread_mutex_unlock(&amp;mutex);

        //do stuff
        for(j=0;j&lt;10000;j++)
            {//some dummy time consuming works}

        sa.sa_flags = SA_SIGINFO;
        sa.sa_sigaction = handler;
        sigemptyset(&amp;sa.sa_mask);
        sigaction(SIG, &amp;sa, NULL);

        sev.sigev_notify = SIGEV_SIGNAL;
        sev.sigev_signo = SIG;
        sev.sigev_value.sival_ptr = &amp;timerid;
        timer_create(CLOCKID, &amp;sev, &amp;timerid);
        /* Start the timer */

        its.it_value.tv_sec = 0;
        its.it_value.tv_nsec = 2000;
        its.it_interval.tv_sec = 0;
        its.it_interval.tv_nsec = 0;

        timer_settime(timerid, 0, &amp;its, NULL);
        pthread_mutex_lock(&amp;mutex);
        state = (state +1)%NUM_THREADS;
        //pthread_cond_signal(&amp;condA[state]);
        pthread_mutex_unlock(&amp;mutex);
        // Timer's code ends
    }

}

int main(int argc, char *argv[])
{
    int data = 0;
    int err;

    while(data &lt; NUM_THREADS)
    {
        //create our threads
        err = pthread_create(&amp;tid[data], NULL, threadA, (void *)(intptr_t)data);
        if(err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
        else
           // printf("\n Thread created successfully\n");

        data++;
    }

    pthread_exit(NULL);
}
</code></pre>

<p>Although no printf statements are executing, why is it freezing after some time?
If no. of timers are limited, what other strategy should I use to redress this issue?</p>