Re: the wait() and pulse() of monitor

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 03/09/04


Date: Tue, 9 Mar 2004 10:08:13 -0000

pt <flybird_fz@21cn.com.AntiSpam> wrote:
> > No, the producer doesn't use wait itself at all. The sequence is:
> >
> > Consumer: Producer:
> > Enter lock
> > Wait (exits lock)
> > Enter lock
> > Pulse
> > Exit lock
> > Regain lock
> > Exit lock
>
> It is just for one time operation. If producer produces more , then
> there will a loop in the block and in the loop pulse and wait will
> be used.

No, the producer would just pulse multiple times. Why would the
producer wait? The idea of this is that the producer is just using
"fire and forget" semantics.
 
> > I think you're missing something, but it's hard to say what it is, I'm
> > afraid.
>
> the explain for pulse is so in msdn:
> Sends a signal to one or more waiting threads. The signal notifies a waiting
> thread that the state of the locked object has changed, and the owner of the
> lock is ready to release the lock. The waiting thread is placed in the
> object's ready queue so that it might eventually receive the lock for the
> object.Once the thread has the lock, it can check the new state of the
> object to see if the required state has been reached.
>
> I think once wait() is called, the thread fall into a state wait and can't
> join the competion for the lock unless being pulsed first.

Yes. (Assuming a timeout isn't specified.)

> so totally I think there are three state:
>
> gain lock, wait and have not been pulsed, waitlock(means just waiting the
> lock,while first enter() or have been pulsed).and pulse() can let thread
> change from wait to waitlock.

Um, I think so.

> I think the wait state introduce more control function, but I would like to
> see which case it should be used. I think it will used while there is more
> threads and the control is complex, but I can't imagine the detail case.

I've shown you a case where it's used - the classic producer/consumer
situation, with a queue of jobs. Please explain how you would tackle
that without wait/pulse (or any similar but equivalent mechanism).

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: the wait() and pulse() of monitor
    ... >> thread waiting for jobs, and another thread which produces jobs to be ... > but the pulse() just mean that the producer is ready to free the control ... > then the worker can get the lock. ...
    (microsoft.public.dotnet.framework)
  • Re: the wait() and pulse() of monitor
    ... > that pulse() and waitis not used, since in the case, the state( ... How exactly do you make the consumer wait until the producer has ... the producer can't hold the lock the whole time and just release it ...
    (microsoft.public.dotnet.framework)
  • Re: .net 2.0 : looking for a "best practice" for multi threading jobs
    ... all threads are "blocked" in the GetNectAction() method ... You don't usually need one lock per variable. ... you pulse all the waiting threads before setting the ...
    (microsoft.public.dotnet.framework)
  • Re: the wait() and pulse() of monitor
    ... the producer doesn't use wait itself at all. ... > Wait (exits lock) ... Sends a signal to one or more waiting threads. ...
    (microsoft.public.dotnet.framework)
  • Re: What is wrong with this Producer-Consumer sample?
    ... that the Thread.Sleepin the producer and in the consumers make the whole ... app sleep instead of only making the current thread sleep. ... You're sleeping while still holding the lock, ... to acquire the lock will have to wait until you've finished sleeping. ...
    (microsoft.public.dotnet.languages.csharp)