Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are haskell channels `Control.Concurrent.Chan` safe for multiple readers/producers?

I need to put together a concurrent system with one shared Control.Concurrent.Chan between threads. There will be only one consumer and many producers. Looking at the Chan documentation I did not see any warning about the number of consumers and producers that may work on the same channel, and the source code seems to be using the default "safe" accessors for MVars, therefore I think it should be safe to assume that there shouldn't be limitations but I'm not sure. So, my question is... do you know whether haskell channels are safe (in general) for multiple readers and producers, please?

like image 725
Riccardo T. Avatar asked Mar 08 '12 10:03

Riccardo T.


1 Answers

They are safe for any number of threads. They are a simple MVar-based linked list. The design trade-offs allow for dupChan which help in the opposite case of broadcasting to multiple readers.

The Chan is so simple it does not count the number of items inside, nor does it have an upper bound. So if the producers outrun the consumer then the Chan will become very very large. If this is a problem then you could pair the Chan with a (MVar Int). and have the producers and consumers modify the running total of items in the Chan.

like image 152
Chris Kuklewicz Avatar answered Nov 16 '22 02:11

Chris Kuklewicz