Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top