pthread_cond_signal Q

This is a discussion on pthread_cond_signal Q within the C Programming forums, part of the General Programming Boards category; I'm trying to understand how using this function doesn't cause a race condition. If I lock a mutex m and ...

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

    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21