Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-08-2008, 08:01 AM
mynickmynick mynickmynick is offline
Newbie
 
Join Date: May 2008
Posts: 11
Rep Power: 0
mynickmynick is on a distinguished road
Default Pthread_cond_wait if cancelled leaves mutex locked deadlocking other threads?!

From the following source 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 08:55 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -5. The time now is 05:13 PM.

Contest Stats

John ........ 87.50000
dargueta ........ 75.00000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads