Return-Path: william@bourbon.usc.edu Delivery-Date: Fri Oct 17 13:06:06 2008 X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on merlot.usc.edu X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.3 Received: from bourbon.usc.edu (bourbon.usc.edu [128.125.9.75]) by merlot.usc.edu (8.14.1/8.14.1) with ESMTP id m9HK66ub001771 for ; Fri, 17 Oct 2008 13:06:06 -0700 Received: from bourbon.usc.edu (localhost.localdomain [127.0.0.1]) by bourbon.usc.edu (8.14.2/8.14.1) with ESMTP id m9HKEax4030056 for ; Fri, 17 Oct 2008 13:14:36 -0700 Message-Id: <200810172014.m9HKEax4030056@bourbon.usc.edu> To: cs551@merlot.usc.edu Subject: Re: CSCI 551- Final project Part 1 :Doubt Date: Fri, 17 Oct 2008 13:14:36 -0700 From: Bill Cheng Someone wrote: > I had a doubt with Signal and Wait. > > Say, there is a Thread X waiting on a Condition Variable CV. > > And there is a Thread Y signaling using CV. > > Ideally, Thread Y would signal and Thread X would wake up from Wait. > > However, if Thread X has not begun waiting yet, and Thread Y comes > along and signals (even when no one is waiting), then how is the > behavior ? Then thread X won't see the signal. > Will the Signal be buffered and used when Thread X executes wait ? No. That's not how pthread signals behave. > In CSCI-402, we used Monitor Variables to overcome this problem. But > here, will Pthreads handle the buffering, or will the signal be lost ? The signal will be lost. You can > Alternatively, can I use Monitor variables here as well ? I'm not sure exactly what you meant by "monitor variables". If you have variables that you want to share among threads, you need to use a mutex. Let's assume what you meant by a "monitor variable" is a variable protected by a mutex. I don't really see how a signal can be missed if you code it correctly! Let's say thread X wants to catch a signal and thread Y wants to send a signal and there is a mutex M and a condition variable C and a "monitor variable" V. So, thread X's code should look something like: pthread_lock(M); while (V does not have the desired value) pthread_cond_wait(M,C); ... Thread Y's code can look something like: pthread_lock(M); /* * you can switch the order of the next two lines of code * and it won't matter because thread Y has the mutex lock. */ pthread_cond_broadcast(C); set V to desired value; pthread_unlock(M); May be you did not signal the condition when the corresponding mutex is locked or you checked the value of V when you did not have the mutex locked. -- Bill Cheng // bill.cheng@usc.edu