public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
* win32 events: generation count solution
@ 2005-05-06  6:15 Hardeep Parmar
  0 siblings, 0 replies; only message in thread
From: Hardeep Parmar @ 2005-05-06  6:15 UTC (permalink / raw)
  To: pthreads-win32

Hi All, 
I am newbie to multithreading. This is in reference to the discussion 
about "Sategies for Implementing POSIX Condition Variables on Win32" at 
following URL. 


http://www.cs.wustl.edu/~schmi­dt/win32-cv-1.html 


I hope a lot of us would have read that article. The discussion 
mentions a Generation count soultion. I shall quote a section on 
pthread_cond_wait implementation in that solution: 


int 
pthread_cond_wait (pthread_cond_t *cv, 
                   pthread_mutex_t *external_mutex) 
{ 
... 
for (;;) { 
    // Wait until the event is signaled. 
    WaitForSingleObject (cv->event_, INFINITE); 


    EnterCriticalSection (&cv->waiters_count_lock_); 
    // Exit the loop when the <cv->event_> is signaled and 
    // there are still waiting threads from this <wait_generation> 
    // that haven't been released from this wait yet. 
    int wait_done = cv->release_count_ > 0 
                    && cv->wait_generation_count_ != my_generation; 
    LeaveCriticalSection (&cv->waiters_count_lock_); 


    if (wait_done) 
      break; 
  } 
EnterCriticalSection (external_mutex); 
... 



} 


AND pthread_cond_signal implementation 

int 
pthread_cond_signal (pthread_cond_t *cv) 
{ 
  EnterCriticalSection (&cv->waiters_count_lock_); 
  if (cv->waiters_count_ > cv->release_count_) { 
    SetEvent (cv->event_); // Signal the manual-reset event. 
    cv->release_count_++; 
    cv->wait_generation_count++; 
  } 
  LeaveCriticalSection (&cv->waiters_count_lock_); 



} 


In the above function once  pthread_cond_signal  calls SetEvent  on a 
manual-reset event, it signals all the waiting threads and hence all of 
them will break free from WaitForSingleObject (...). 
      Assuming the all progress at same pace they will all break out of 
"for" loop and would start waiting at EnterCriticalSection 
(external_mutex) competing to acquire the mutex i.e it is possible that 
ResetEvent is called after more than one thread have broken out of for 
loop. Hence in this case pthread_cond_signal does not work as expected 
i.e. it wakes up more than one thread where as it should have worken up 
only one of all the waiting threads. 
What could be wrong in my interpretation here? Thus , My conclusion 
here is that Generation Count is not an applicable(for consideration) 
solution unless above issues is addressed(e.g create two events one 
autoreset and another manual and trigger only autoreset in case of 
pthread_cond_signal). 

Thanks

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2005-05-06  6:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-06  6:15 win32 events: generation count solution Hardeep Parmar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).