Thread: pthread_cond_signal Q

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    pthread_cond_signal Q

    I'm trying to understand how using this function doesn't cause a race condition. If I lock a mutex m and then call pthread_cond_wait(cond, m) m is now released and some other thread can lock it. When pthread_cond_signal(cond) is called, the first thread now locks the mutex now there are 2 threads running code between a lock/release on the same mutex. Am I missing something or should all code after the pthread_cond_wait be (I think this is the correct term) thread safe?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by homer_3 View Post
    When pthread_cond_signal(cond) is called, the first thread now locks the mutex now there are 2 threads running code between a lock/release on the same mutex.
    Incorrect. pthread_cond_signal() causes the thread to wake up and attempt to acquire the mutex. It will block until the thread which called pthread_cond_signal() deliberately releases it. The mutex is never locked by two threads at once -- that is impossible.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed