Thanks for getting me to lookup Monitors (Wait / Pulse) again.  I forgot
about them ages ago.

A Monitor is like a Mutex in that it guards a critical section so that it
can only be executed by one thread at a time.

I'm specifically after the counting functionality of a Semaphore so that
consumer threads block when the buffer becomes empty, and are resumed when
items are eventually added.

See
http://stackoverflow.com/questions/301160/what-are-the-differences-between-various-threading-synchronization-options-in-c

It seems that the only way to achieve what I want would be to use a Monitor
(or lock(object) { ... } construct) to guard access to the Semaphore but
this negates the usefulness of the Semaphore.

I think the best options are to swallow the exception and return null, or
never allow the buffer to be cleared (i.e. force it to be reconstructed
instead).

Cheers.

On Tue, May 18, 2010 at 7:31 PM, Matt Siebert <[email protected]> wrote:

> Release signals the consumers.
>
> Wait/Pulse sounds pretty much like a semaphore but I'll take a look later.
>
> I'm thinking my buffer.GetNextObject() can just swallow the exception
> and return null which is the expected behaviour if the buffer is
> empty. Seems ok but I'd prefer not to cause an exception...
>
> On Tuesday, May 18, 2010, silky <[email protected]> wrote:
> > On Tue, May 18, 2010 at 5:29 PM, Matt Siebert <[email protected]>
> wrote:
> >> Hi folks,
> >>
> >> I've got a producer / consumer scenario and I've implemented a buffer
> class
> >> that wraps a collection and exposes a method for consumers to retrieve
> >> objects from the buffer.  It uses a semaphore to provide a wait handle
> for
> >> the retrieval so that consumer threads will just block (with a timeout)
> >> until there is something to consume.
> >>
> >> This all works nicely, until I need to clear the buffer.  When I clear
> the
> >> buffer I need to reset the semaphore's count back to 0, but Semaphore
> >> doesn't seem to expose this functionality.  It looks like I need to
> close
> >> the semaphore and create a new one, but if consumers are waiting on it
> they
> >> get a ObjectDisposedException ('Safe handle has been closed').
> >>
> >> Anyone handled this kind of scenario before?  I'm beginning to think I
> need
> >> to detach the consumers, dispose and recreate the buffer and then attach
> the
> >> consumers again...
> >
> > When I've done queue-based things I've used the Wait/Pulse system:
> >
> >
> http://www.dotnetspider.com/resources/443-read-Communication-Using-Wait-Pulse-Pul.aspx
> >
> > I've never used the Semaphore class before, but from:
> > http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx,
> > can you not all "Release" passing in the maximum size? (I may have
> > misunderstood).
> >
> >
> >> Cheers,
> >> Matt.
> >
> > --
> > silky
> >
> >   http://www.programmingbranch.com/
> >
>

Reply via email to