Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 8th, 2008, 8:03 AM   #1
mynickmynick
Newbie
 
Join Date: May 2008
Posts: 7
Rep Power: 0 mynickmynick is on a distinguished road
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 8:27 AM.
mynickmynick is offline   Reply With Quote
Old May 8th, 2008, 8:22 AM   #2
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 118
Rep Power: 1 BstrucT is on a distinguished road
Send a message via MSN to BstrucT
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
__________________
>BstrucT
BstrucT is offline   Reply With Quote
Old May 8th, 2008, 8:59 AM   #3
mynickmynick
Newbie
 
Join Date: May 2008
Posts: 7
Rep Power: 0 mynickmynick is on a distinguished road
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?
mynickmynick is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto-locking old threads Jimbo Community Announcements and Feedback 15 May 21st, 2006 9:31 AM
Evaporating threads DaWei Coder's Corner Lounge 20 Nov 12th, 2005 9:26 AM
Share Variables Between Threads zyd C 5 Nov 3rd, 2005 8:13 PM
Unanswered Threads java_roshan Community Announcements and Feedback 1 Aug 7th, 2005 9:43 AM
Multiple server clients using sockets + Threads captainK Java 3 Mar 4th, 2005 10:10 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:47 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC