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