In System.Threading.Channels
, there's a ChannelOptions.SingleReader
property that selects an optimized implementation if there can only be one read issued at a time.
ChannelOptions
is inherited by BoundedChannelOptions
which has the FullMode
property.
Two of the FullMode
values are able to remove items from the Channel's internal storage/queue:
BoundedChannelFullMode.DropNewest
Removes and ignores the newest item in the channel in order to make room for the item being written.
BoundedChannelFullMode.DropOldest
Removes and ignores the oldest item in the channel in order to make room for the item being written.
Does this removal of the newest/oldest item, which occurs during a write, count as a reader for the purposes of the SingleReader
property?
(That wouldn't make these two values mutually exclusive with SingleReader = true
, but it would mean that when used in combination, not only can two reads not overlap each other, but a read also can't overlap a write.)
CodePudding user response:
Configuring a bounded channel with the SingleWriter
and SingleReader
options has only a decorative effect. Currently (.NET 7) there is only one bounded Channel<T>
implementation, which supports multiple writers and readers. This implementation is based on a Deque<T>
(internal .NET type similar to a Queue<T>
) synchronized with a lock
. You can set the SingleWriter
/SingleReader
options to any combination of values, and it will make absolutely no difference. The SingleReader
option has effect only on unbounded channels, and the SingleWriter
hasn't any effect anywhere.
So does the removal of the newest/oldest item, which occurs during a write, count as a reader? The docs don't specify it explicitly. The documentation of the Channels library is somewhat lacking in general. My opinion is that it shouldn't count as a reader. In a mechanism where the roles of writing and reading are so carefully separated, It would be highly surprising if a simple act of writing was actually a writing reading hybrid. But since the SingleWriter
/SingleReader
options have no effect on bounded channels, me expressing an academic opinion and arguing about it is probably an exercise to futility.