Removing the contents of a Chan or MVar in a single discrete step

Posted by Bill on Stack Overflow See other posts from Stack Overflow or by Bill
Published on 2010-12-31T02:47:09Z Indexed on 2010/12/31 2:54 UTC
Read the original article Hit count: 470

Filed under:

I'm writing a discrete simulation where request values from multiple threads accumulate in a centralized queue. Every n milliseconds, a manager wakes up to process requests. When the manager wakes up, it should retrieve all of the contents of the central queue in a single discrete step. While processing these, any client threads attempting to submit to the queue should block. When processing completes, the queue reopens and the manager goes back to sleep.

What's the best way to do this? The retry behavior of STM isn't really what I want. If I use a Chan or MVar, there's no way to prevent clients from enqueuing additional requests during processing. One approach is to use an MVar as a mutex on a Chan holding the queue. Are there other ways to do this?

© Stack Overflow or respective owner

Related posts about haskell