This is the mail archive of the mailing list for the pthreas-win32 project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

win32 events: generation count solution

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.­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: 

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) 
EnterCriticalSection (external_mutex); 


AND pthread_cond_signal implementation 

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. 
  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 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]