From: Hardeep Parmar <hardeep.parmar@gmail.com>
To: pthreads-win32@sources.redhat.com
Subject: win32 events: generation count solution
Date: Fri, 06 May 2005 06:15:00 -0000 [thread overview]
Message-ID: <2604e697050505231567b76d75@mail.gmail.com> (raw)
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
reply other threads:[~2005-05-06 6:15 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2604e697050505231567b76d75@mail.gmail.com \
--to=hardeep.parmar@gmail.com \
--cc=pthreads-win32@sources.redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).