Implementing a Haskell Lock Queue

There is a nice java.util.concurrent package in java that contains an implementation for the BlockingQueue interface.

I need something like this in Haskell so that it can

  • maintain a fixed queue size in memory
  • read block operations when the queue is empty (get)
  • provide blocks with a temporary block that will return Nothing if the queue is empty and the timeout is exceeded
  • similar for input operations - block until the queue has a capacity with a temporary version

perhaps this can be implemented using STM or transaction blocking, but I could not find something like this in hackage.

+4
source share
3 answers

The stm-chans package contains a large number of channels for STM. It seems to be more actively supported than the aforementioned Houndar package (which was last updated in 2009), and thanks to the use of STM it will be safe.

I believe that its variant of TBChan , combined with System.Timeout , meets all your requirements.

+3
source

The parallel queue is often called the Chan in Haskell, and as you might expect, there really is a BoundedChan package in Hackage that looks like it should fit your needs, with the exception of timeouts. However, you can get this using System.Timeout .

+7
source

I need to give a little warning. A source from BoundedChan shows that this is no exception. If you know that you are free of exceptions, for example, you avoid killThread, then everything will be fine. If you need a bulletproof library, you will need to improve BoundedChan . An exceptionally secure library will use withMVar or bracket instead of takeMVar and putMVar .

Using STM avoids most of the exception security problem, and this can be compiled using System.Timeout . In addition, the timeout was wrapped in several ways on Hackage .

+3
source

Source: https://habr.com/ru/post/1396026/


All Articles