From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26292 invoked by alias); 6 May 2005 06:15:19 -0000 Mailing-List: contact pthreads-win32-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: pthreads-win32-owner@sources.redhat.com Received: (qmail 26093 invoked from network); 6 May 2005 06:15:11 -0000 Received: from unknown (HELO wproxy.gmail.com) (64.233.184.201) by sourceware.org with SMTP; 6 May 2005 06:15:11 -0000 Received: by wproxy.gmail.com with SMTP id 69so829882wri for ; Thu, 05 May 2005 23:15:11 -0700 (PDT) Received: by 10.54.125.17 with SMTP id x17mr618612wrc; Thu, 05 May 2005 23:15:11 -0700 (PDT) Received: by 10.54.49.11 with HTTP; Thu, 5 May 2005 23:15:11 -0700 (PDT) Message-ID: <2604e697050505231567b76d75@mail.gmail.com> Date: Fri, 06 May 2005 06:15:00 -0000 From: Hardeep Parmar Reply-To: Hardeep Parmar To: pthreads-win32@sources.redhat.com Subject: win32 events: generation count solution Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-SW-Source: 2005/txt/msg00085.txt.bz2 Hi All,=20 I am newbie to multithreading. This is in reference to the discussion=20 about "Sategies for Implementing POSIX Condition Variables on Win32" at=20 following URL.=20 http://www.cs.wustl.edu/~schmi=ADdt/win32-cv-1.html=20 I hope a lot of us would have read that article. The discussion=20 mentions a Generation count soultion. I shall quote a section on=20 pthread_cond_wait implementation in that solution:=20 int=20 pthread_cond_wait (pthread_cond_t *cv,=20 pthread_mutex_t *external_mutex)=20 {=20 ...=20 for (;;) {=20 // Wait until the event is signaled.=20 WaitForSingleObject (cv->event_, INFINITE);=20 EnterCriticalSection (&cv->waiters_count_lock_);=20 // Exit the loop when the event_> is signaled and=20 // there are still waiting threads from this =20 // that haven't been released from this wait yet.=20 int wait_done =3D cv->release_count_ > 0=20 && cv->wait_generation_count_ !=3D my_generation;=20 LeaveCriticalSection (&cv->waiters_count_lock_);=20 if (wait_done)=20 break;=20 }=20 EnterCriticalSection (external_mutex);=20 ...=20 }=20 AND pthread_cond_signal implementation=20 int=20 pthread_cond_signal (pthread_cond_t *cv)=20 {=20 EnterCriticalSection (&cv->waiters_count_lock_);=20 if (cv->waiters_count_ > cv->release_count_) {=20 SetEvent (cv->event_); // Signal the manual-reset event.=20 cv->release_count_++;=20 cv->wait_generation_count++;=20 }=20 LeaveCriticalSection (&cv->waiters_count_lock_);=20 }=20 In the above function once pthread_cond_signal calls SetEvent on a=20 manual-reset event, it signals all the waiting threads and hence all of=20 them will break free from WaitForSingleObject (...).=20 Assuming the all progress at same pace they will all break out of=20 "for" loop and would start waiting at EnterCriticalSection=20 (external_mutex) competing to acquire the mutex i.e it is possible that=20 ResetEvent is called after more than one thread have broken out of for=20 loop. Hence in this case pthread_cond_signal does not work as expected=20 i.e. it wakes up more than one thread where as it should have worken up=20 only one of all the waiting threads.=20 What could be wrong in my interpretation here? Thus , My conclusion=20 here is that Generation Count is not an applicable(for consideration)=20 solution unless above issues is addressed(e.g create two events one=20 autoreset and another manual and trigger only autoreset in case of=20 pthread_cond_signal).=20 Thanks