pthread_cond_timedwait call is not working with mutex locking mechanism
<p>I have a logging application. In which after specific time (user configureable time) i need to close the current log file and and create a new log file and log the data. </p>

<p>There are 2 critical sections of code in my program. </p>

<ol>
<li>Writing the data to file (in <code>while(1)</code>)</li>
<li>Close and open the new file</li>
</ol>

<p>Whenever time expires i need to stop the logging to the file and close it &amp; open the new log file and start logging.</p>

<p>For this purpose i have created a sample application exactly same as my application, but instead of file operation here am doing with <code>printf</code>.</p>

<p>I am using <code>pthread_cond_timedwait</code> call to handle the timer related operation. Here is my sample code - </p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;sys/time.h&gt;
#include &lt;errno.h&gt;
#include &lt;pthread.h&gt;

/* For safe condition variable usage, must use a boolean predicate and  */
/* a mutex with the condition.                                          */
pthread_cond_t      cond  = PTHREAD_COND_INITIALIZER;
pthread_mutex_t     mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t     lock  = PTHREAD_MUTEX_INITIALIZER;
#define WAIT_TIME_SECONDS       10

void *threadfunc(void *parm)
{
    int               rc;
    struct timespec   abstime;
    struct timeval    now;

    /* Usually worker threads will loop on these operations */
    while (1) {
        rc =  gettimeofday(&amp;now, NULL);

        /* Convert from timeval to timespec */
        abstime.tv_sec = now.tv_sec + WAIT_TIME_SECONDS;
        abstime.tv_nsec = (now.tv_usec + 1000UL * WAIT_TIME_SECONDS) * 1000UL;
        abstime.tv_sec += abstime.tv_nsec / (1000 * 1000 * 1000);
        abstime.tv_nsec %= (1000 * 1000 * 1000);

        printf("Thread blocked\n");
        pthread_mutex_lock(&amp;mutex);
        rc = pthread_cond_timedwait(&amp;cond, &amp;mutex, &amp;abstime);
        pthread_mutex_unlock(&amp;mutex);
        printf("Wait timed out!\n");
        /* critical section of code */
        pthread_mutex_lock(&amp;lock);
        printf("Thread consumes work here\n");
        pthread_mutex_unlock(&amp;lock);
    }

    return NULL;
}

int main(int argc, char **argv)
{
    int rc=0;
    pthread_t threadid;

    rc = pthread_create(&amp;threadid, NULL, threadfunc, NULL);
    if (rc != 0) {
        printf("Thread creation failed err:%d\n", errno);
        return -1;
    }
    while (1) {
        /* critical section of code */
        sleep(1);
        pthread_mutex_lock(&amp;lock);
        printf("One work item to give to a thread\n");
        pthread_mutex_unlock(&amp;lock);
    }

    printf("Wait for threads and cleanup\n");
    pthread_join(threadid, NULL);

    pthread_cond_destroy(&amp;cond);
    pthread_mutex_destroy(&amp;mutex);
    printf("Main completed\n");
    return 0;
}
</code></pre>

<p>This code is not working. Whenever <code>pthread_cond_timedwait</code> expires <code>threadfunc</code> function should acquire mutex <code>lock</code> and it needs print the statements. After printing am releasing the mutex. But it is not happening. Control is never transfered from <code>main()</code> to <code>threadfunc</code>. Whats is going wrong here?</p>

<p>But instead of mutex if i use a variable to do the handling of critical section of code am able to do it. Say -</p>

<pre><code>static int stop = 0; // take a global variable

//inside threadfunc
/* critical section of code */
stop = 1; //Replace pthread_mutex_lock with this 
printf("Thread consumes work here\n");
stop = 0; //Replace pthread_mutex_unlock with this 

//in main()
while (1) {
    /* critical section of code */
    sleep(1);
    if (!stop)
        printf("One work item to give to a thread\n");
}
</code></pre>

<p>This approach is working fine. </p>

<p>When i am using more then one mutex in my code am not able to implement this. What is the real thing happening behind? Am i missing anything while am using mutex to handle critical section of a code? </p>

<p>Thanks in advance!</p>