0
votes

Man pages say this pthread_cond_wait atomically unlocks the mutex (as per pthread_unlock_mutex) and waits for the condition variable cond to be signaled.
But then thread start_function could be entered by any other thread causing two threads to be in the critical block.

    void* start_function(){
         pthread_mutex_lock(&mutex);
         // critical block
         pthread_mutex_unlock(&mutex);
    }
    
    int main(){
// code to create threads
        pthread_cond_wait(&cond, &mutex);
    }
    

So in the above code after it hits pthread_cond_wait it will unlock the mutex ?

2
That is undefined behaviour. The mutex MUST be locked by the thread calling pthread_cond_wait exactly to prevent unpredictable results. - Cheatah
That is the whole point of unlocking, it allows others to access the resource associated with the mutex. - Ulrich Eckhardt

2 Answers

0
votes

You dont need to have a condition when you creating threads .In Mutual Exclusion only one process can run critical area at a time.A process remains in the critical area finite time and when its done its work and no one process remains in the critical area then with condition signals the other thread is ready to run also this critical area.All this work is done to avoid impasse.Also when a process stops outside of critical block it must not interacts with other processes.So your example must be :

void* start_function(){
         pthread_mutex_lock(&mutex);
           pthread_cond_wait(&cond, &mutex);
         // critical block or critical area

         // you can use signals here 
         pthread_mutex_unlock(&mutex);
    }
    
    int main(){
        // code to create threads
        for(int i=0;i<10;i++){
             pthreaf_create(x[i],&start_function,...,NULL);
       }
    }

It is probably at the end of the program some pthreads have been stuck at con_wait() , so you need a broadcast() to free all threads

0
votes

Man pages say this pthread_cond_wait atomically unlocks the mutex (as per pthread_unlock_mutex) and waits for the condition variable cond to be signaled.

Yes, right after they say that that function

shall be called with mutex locked by the calling thread or undefined behavior results.

(emphasis added). You go on to remark,

But then thread start_function could be entered by any other thread causing two threads to be in the critical block.

If you invoke undefined behavior by calling pthread_cond_wait() without holding the specified mutex then anything can happen -- the behavior is undefined. It is possible that the undefined behavior manifests simply as two threads executing in the critical region at the same time, with whatever follow-on effects that may have, but it is also possible that the program crashes, then or later, or corrupts memory, or deadlocks, or tweets all your passwords, among a universe of possibilities.

So in the above code after it hits pthread_cond_wait it will unlock the mutex ?

Impossible to say. Again, the behavior is undefined.