Thread: Pthread_cond_wait if cancelled leaves mutex locked deadlocking other threads?!

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    Pthread_cond_wait if cancelled leaves mutex locked deadlocking other threads?!

    From the following source code
    Code:
    int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
    {
    volatile pthread_descr self = thread_self();
    
    __pthread_lock(&cond->__c_lock, self);
    enqueue(&cond->__c_waiting, self);
    __pthread_unlock(&cond->__c_lock);
    pthread_mutex_unlock(mutex);
    suspend_with_cancellation(self);
    pthread_mutex_lock(mutex);
    /* This is a cancellation point */
    if (THREAD_GETMEM(self, p_canceled)
    && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
    /* Remove ourselves from the waiting queue if we're still on it */
    __pthread_lock(&cond->__c_lock, self);
    remove_from_queue(&cond->__c_waiting, self);
    __pthread_unlock(&cond->__c_lock);
    pthread_exit(PTHREAD_CANCELED);
    }
    return 0;
    }
    it seems that when a thread gets canceled while waiting for cond it performs
    pthread_mutex_lock(mutex);
    ...
    pthread_exit(PTHREAD_CANCELED);

    and leaves mutex locked?!
    so anothe thread is potentially deadlocked?!
    I tried to look in the source for pthread_exit() if and when the mutex is released but I didn't figure out!


    So may be to prevent this the correct usage is

    Code:
      pthread_mutex_lock(mutex);
      pthread_cleanup_push(pthread_mutex_unlock,mutex);
      while (...) pthread_cond_wait(cond,mutex); 
    ......
      pthread_cleanup_pop();
      pthread_mutex_unlock(mutex);
    what you think?
    Last edited by mynickmynick; 05-08-2008 at 07:56 AM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mynickmynick View Post
    So may be to prevent this the correct usage is

    Code:
      pthread_mutex_lock(mutex);
      pthread_cleanup_push(pthread_mutex_unlock,mutex);
      while (...) pthread_cond_wait(cond,mutex); 
    ......
      pthread_cleanup_pop();
      pthread_mutex_unlock(mutex);
    what you think?
    Almost right, except the call to pthread_cleanup_pop should look like:

    Code:
    pthread_cleanup_pop(0);
    pthread_mutex_unlock(mutex);
    Or, just allow the cleanup handler to run itself when you pop it, unlocking the mutex for you:

    Code:
    pthread_cleanup_pop(1);

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    ok great
    thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  3. Threading and mutex's
    By megatron09 in forum C++ Programming
    Replies: 14
    Last Post: 09-07-2006, 02:40 PM
  4. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  5. Producer-comsumer threads using semaphore mutex
    By Kaiya in forum C Programming
    Replies: 3
    Last Post: 04-29-2004, 08:17 AM