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

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


Date: Thu, 11 Mar 2004 08:05:03 -0000

pt <flybird_fz@21cn.com.AntiSpam> wrote:
> I just think in the producer/consumer case, the complex corporation
> that pulse() and wait() is not used, since in the case, the state(
> waited but haved not been pulsed) for consumer has no special effect,
> and the state is no necessary in the case.

How exactly do you make the consumer wait until the producer has
produced something and reliably then consume it?

> and the producer has
> to use two step pulse() and exit() or wait to do the work, and
> with other machanism such as samaphore, just one step does the work.

No, the producer can't hold the lock the whole time and just release it
to effectively pulse the consumer, for two reasons:

1) There may well be more than one producer
2) There's no guarantee that *as soon as* the producer releases the
lock, the consumer will get it. If the producer is then going to
acquire the lock again, that may happen before the consumer acquires
it.

Nothing should hold locks for long periods of time, in my view, but I
*think* that's what you're proposing.

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


Relevant Pages

  • Re: A design question (around threading)
    ... my application needs to be able to scan a document and upload it ... it sounds as though you need a "producer" thread that handles ... and a "consumer" thread that handles uploading. ... the lock and then enter a work loop. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A design question (around threading)
    ... Now, I want to take advantage of the sheet feeder, as there is an initial tranch of about 50,000 documents to scan and upload, so if I get the system set up optimally, I will get a scan every 4 seconds. ... Based on the information you've provided so far, it sounds as though you need a "producer" thread that handles scanning, and a "consumer" thread that handles uploading. ... The consumer thread will acquire the lock and then enter a work loop. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Multithreading - Problem with notifyAll() and wait()
    ... NotifyAlland waitstatements (producer notifies and consumer ... private Queue queue = new Queue; ... Vector vectorQueue = new Vector; ...
    (comp.lang.java.programmer)
  • Re: Gas prices up to pay for bosses
    ... kilter profits become the more competition intensified. ... of the producer ought to be attended to, only so far as it may be necessary ... for promoting that of the consumer. ...
    (uk.politics.misc)
  • Re: Is this group dying?
    ... consumer queue toward the dispatcher thread and a single-producer/ ... that producer has put a new "next" node in place. ... I want to implement the wait-free factory as a multi-threaded daemon process ...
    (comp.programming.threads)

Loading