8

The documentation for Channel.CreateUnbounded says:

Creates an unbounded channel usable by any number of readers and writers concurrently.

However Channel has properties for a single ChannelReader and ChannelWriter only, and there doesn't appear to be a way to create a reader/writer explicitly around an existing channel.

I had thought that if you had multiple producers/consumers they should share the same instance of a writer/reader, is this incorrect? Is the "number of readers/writers" talking about concurrent access rather than number of class instances?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589

1 Answers1

16

Yes, the documentation means multiple producers (writers) and consumers (readers). All producers are allowed to use the single ChannelWriter of the channel, and all consumers are allowed to use its single ChannelReader. No external synchronization is required. The Channel<T> class is 100% thread-safe.

Important: In case of multiple consumers, each item passed through the channel will be received by only one consumer. The Channel<T> doesn't support propagating one element to multiple consumers, like it does the BroadcastBlock<T> dataflow block for example.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104