jberryman / unagi-chan

A haskell library implementing fast and scalable concurrent queues for x86, with a Chan-like API
BSD 3-Clause "New" or "Revised" License
127 stars 15 forks source link

Blocking read available contents of Chan to list #44

Closed Diamondy4 closed 1 year ago

Diamondy4 commented 2 years ago

Is there a way to flush chan contents to a list? tryReadChan is definitely not what I want, as it removes elements from chan to Element with no way to place it back. NoBlocking version's stream can do what I want, but I need multiple producers/multiple consumers, where each consumer can try to flush whole Chan to list. I think I can go around it using MVar around OutChan, but that feels dirty. Is there anything like flushTBQueue that I missed?

jberryman commented 2 years ago

You want the flush operation to never block right?

Hmm If you know you there are no concurrent readers running at the time then estimatedLength would give you the lower bound of items in the queue. But you need this to work with multiple consumers.

I can't really think of a way to make this work within the current design

Diamondy4 commented 2 years ago

I have multiple consumers running every X seconds (at different time), who should flush whole chan contents into some IO action, so I think - yes - I want to never block, and want return immediately contents of chan (or [] when empty). TBQueue flushTBQueue is good but slow :<