jdevelop
jdevelop

Reputation: 12296

Blocking queue implementation in Haskell

In java, there is nice package java.util.concurrent which holds implementation for BlockingQueue interface.

I need something similar in Haskell, so it will be able to

probably this could be implemented with STM or blocking transactions - but I was not able to find something like that on hackage.

Upvotes: 6

Views: 1033

Answers (3)

ehird
ehird

Reputation: 40787

The stm-chans package contains a wide variety of channels for STM. It seems to be more actively maintained than the BoundedChan package hammar mentioned (which was last updated in 2009), and thanks to its use of STM, it'll be exception-safe.

I believe its TBChan variant, in conjunction with System.Timeout, meets all your requirements.

Upvotes: 3

Chris Kuklewicz
Chris Kuklewicz

Reputation: 8153

I need to give a small warning. The source of BoundedChan shows that it is not exception safe. If you know you are exception free, for example you avoid killThread, then this will be okay. If you want a bulletproof library then you will have to improve on BoundedChan. An exception safe library will be using withMVar or bracket instead of takeMVar and putMVar.

Using STM would avoid most of the exception safety issue, and this can be composed with System.Timeout. Also, timeout has been wrapped a few ways on Hackage.

Upvotes: 3

hammar
hammar

Reputation: 139840

A concurrent queue is often called a Chan (channel) in Haskell, and as you might expect, there is indeed a BoundedChan package on Hackage which looks like it should fit your needs, except for timeouts. However, you should be able to get that by using System.Timeout.

Upvotes: 7

Related Questions