![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2008
Posts: 7
Rep Power: 0
![]() |
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! Last edited by mynickmynick; May 8th, 2008 at 9:27 AM. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 198
Rep Power: 1
![]() |
Re: pthread_cond_wait if cancelled leaves mutex locked deadlocking other threads?!
You might want to check out how to post code properly before you get flamed.
>BstrucT
__________________
Be kinder than necessary because everyone you meet is fighting some kind of battle. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2008
Posts: 7
Rep Power: 0
![]() |
Re: pthread_cond_wait if cancelled leaves mutex locked deadlocking other threads?!
So may be to prevent this the correct usage is
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? |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 198
Rep Power: 1
![]() |
Re: pthread_cond_wait if cancelled leaves mutex locked deadlocking other threads?!
Noted your comment to thread as unpleasant.
I apologize for the rude reply I have made to your thread, that was dumb. But it is important to note how to format code properly, as I have been tapped accross the fingers for the same mistake, a few times. I wasn't trying to be rude, just trying to give you a heads up man. My apologies if it went down wrong.
__________________
Be kinder than necessary because everyone you meet is fighting some kind of battle. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Auto-locking old threads | Jimbo | Community Announcements and Feedback | 15 | May 21st, 2006 10:31 AM |
| Evaporating threads | DaWei | Coder's Corner Lounge | 20 | Nov 12th, 2005 10:26 AM |
| Share Variables Between Threads | zyd | C | 5 | Nov 3rd, 2005 9:13 PM |
| Unanswered Threads | java_roshan | Community Announcements and Feedback | 1 | Aug 7th, 2005 10:43 AM |
| Multiple server clients using sockets + Threads | captainK | Java | 3 | Mar 4th, 2005 11:10 AM |