当前位置: 动力学知识库 > 问答 > 编程问答 >

concurrency - 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?

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: