This is the mail archive of the
pthreads-win32@sources.redhat.com
mailing list for the pthreas-win32 project.
win32 events: generation count solution
- From: Hardeep Parmar <hardeep dot parmar at gmail dot com>
- To: pthreads-win32 at sources dot redhat dot com
- Date: Fri, 6 May 2005 11:45:11 +0530
- Subject: win32 events: generation count solution
- Reply-to: Hardeep Parmar <hardeep dot parmar at gmail dot com>
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/~schmidt/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