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

From: pt (flybird_fz_at_21cn.com.AntiSpam)
Date: 03/09/04


Date: Tue, 9 Mar 2004 17:03:57 +0800


"Jon Skeet [C# MVP]" <skeet@pobox.com> ????
news:MPG.1ab78b802f81047298a37b@msnews.microsoft.com...
...
> 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.

> The lock isn't held by anyone for long.
>
> > So there is three state for the worker
> >
> > wait--pulsed(don't get the lock and still can do nothing)--get lock and
work
> >
> > I just wonder why introduced the middle state--pulsed? I still think
> > in this case it is not necessary.
>
> 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.

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.

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.

regards,
PanTao



Relevant Pages

  • notify / notifyAll misunderstanding
    ... notify wakes up a waiting thread that then attempts to ... waiting threads and ONLY ONE gains the lock, the others go back to waiting. ... When unlockis called a record is removed from the set and notifyAll() is ... So 8 waiting threads are then all notified and 1 arbitraily gets the lock. ...
    (comp.lang.java.programmer)
  • Re: the wait() and pulse() of monitor
    ... >> Regain lock ... the producer would just pulse multiple times. ... > Sends a signal to one or more waiting threads. ...
    (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: notify / notifyAll misunderstanding
    ... I.e. if a lock for a particular record is ... > you want one of the waiting threads to reliably get the lock? ... Yes, I was just using it to understand the notifyAll(), which I now do ... Mike W ...
    (comp.lang.java.programmer)
  • 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)