Questions tagged [system.threading.channels]

Provides a set of highly scalable, low/no-allocation synchronization data structures for passing data between producers and consumers asynchronously.


System.Threading.Channels contains the abstractions and base implementation for async friendly queues as a replacement for synchronous thread-safe BlockingCollection and ConcurrentQueue.

It's also a partial replacement for the System.Threading.Tasks.Dataflow async friendly queues (which is implicitly implemented inside BufferBlock and other blocks).

The API Proposal is probably the only official documentation at the moment.

Exploring System.Threading.Channels offers a good overview of channels

Decoupling

Publishers and subscribers can only access a channel through a ChannelWriter and ChannelReader respectively. This decouples publishers, subscribers and the channel implementation itself.

Backpressure

A Bounded Channel allows only a specific number of items. Its behaviour when full depends on the BoundedChannelFullMode option. By default, publishers will have to wait asynchronously if a channel becomes full.

Other values of the BoundedChannelFullMode enum allow different behaviours. For example, DropOldest can be used to create a circular buffer. DropWrite can be used to throttle publishers by rejecting overflowing messages.

65 questions
2
votes
3 answers

How to implement the BlockingCollection.TakeFromAny equivalent for Channels?

I am trying to implement an asynchronous method that takes an array of ChannelReaders, and takes a value from any of the channels that has an item available. It is a method with similar functionality with the BlockingCollection.TakeFromAny…
2
votes
2 answers

Is SemaphoreSlim needed when Channel's SingleReader is set to true

When sending data fast enough, InvalidOperationException is being thrown: 'There is already one outstanding 'SendAsync' call for this WebSocket instance. ClientWebSocket.ReceiveAsync and ClientWebSocket.SendAsync can be called simultaneously, but at…
nop
  • 4,711
  • 6
  • 32
  • 93
2
votes
2 answers

Consume all messages in a System.Threading.Channels.Channel

Suppose I have a many producers, 1 consumer unbound Channel, with a consumer: await foreach (var message in channel.Reader.ReadAllAsync(cts.Token)) { await consume(message); } The problem is that the consume function does some IO access and…
Guiorgy
  • 1,405
  • 9
  • 26
2
votes
2 answers

Channel multiple producers and consumers

I have the below code: var channel = Channel.CreateUnbounded(); var consumers = Enumerable .Range(1, 5) .Select(consumerNumber => Task.Run(async () => { var rnd = new Random(); while (await…
Houlahan
  • 783
  • 3
  • 19
  • 46
2
votes
3 answers

Channels with CancellationTokenSource with timeout memory leak after dispose

The complete reproducible code is on github, the memory will soon rocket after launching the executable. The code resides mostly in AsyncBlockingQueue.cs class. The following code implements a simple async "blocking" queue: public async…
HooYao
  • 554
  • 5
  • 19
2
votes
1 answer

Is "system.threading.channels" will solve the pub sub problem for cross process applications

I have a asp.net core application "A" which generates files every 1 minute in a folder. Application "B" wants a notification or file details what file we generated and some Hash information for that file. Based on this notification, Application "B"…
user584018
  • 10,186
  • 15
  • 74
  • 160
2
votes
3 answers

Is it possible and/or advisable to use multiple System.Threading.Channels in one Object?

I am working on a .net core 3.0 web application and have decided to use System.Threading.Channels in a singleton service. The top level of my scoped request services injects this singleton to access its channels. I have decided to use this pattern…
pweber
  • 31
  • 1
  • 2
2
votes
2 answers

Wait async for a event from a never ending Task

Currently I'am build a webserver that needs to wait from incoming messages, but i need to wait asynchronisally for these messages. These messages are received in an other Task that keeps running. The endless task pushes Events and the other Task…
Tragamota
  • 131
  • 1
  • 8
2
votes
1 answer

What is the meaning of underscore in _ = WriteItems(channel.Writer, count, delay);

I'm reading about SignalR and I have found code: public ChannelReader Counter(int count, int delay) { var channel = Channel.CreateUnbounded(); // We don't want to await WriteItems, otherwise we'd end up waiting // for all…
Zet
  • 571
  • 3
  • 13
  • 31
1
vote
1 answer

Does BoundedChannelFullMode count as additional readers?

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…
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1
vote
1 answer

Why a Channel needs a CancellationToken if it has Complete method?

I'm using System.Threading.Channel. The Channel.Reader.WaitToReadAsync accepts a CancelationToken. But, there is also Channel.Writer.TryComplete() or Channel.Writer.Complete() methods. So, why would I use a canelation token to stop waiting if I just…
theateist
  • 13,879
  • 17
  • 69
  • 109
1
vote
2 answers

Channel/BlockingCollection alloc free alternatives?

I recently benchmarked my framework and noticed that it allocates tons of garbage. I'm using a Channel and the TryRead or ReadAsync operation allocates memory every single call. So I exchanged that with a BlockingCollection which also…
1
vote
1 answer

Exception handling in Open.ChannelExtensions pipelines

I'm having issues with proper handling of exceptions thrown from pipeline steps created using Open.ChannelExtensions library. In some scenarios exception is being swallowed instead of being propagated to caller. From my observations, it seems that…
1
vote
2 answers

Use two background service to produce and consume Channel for to cache messages

I'm working on project with a background service consume messages from Rabbitmq's queue. I have a background service use background task queue like this and here to process task paralleling. I would like to store in buffer messages consumed and…
1
vote
2 answers

Should we use ValueTask within System.Threading.Channels.WaitToReadAsync loops?

Since we expect to be reading frequently, and for us to often be reading when data is already available to be consumed, should SendLoopAsync return ValueTask rather than Task, so that we can make it allocation-free? // Caller _ =…
nop
  • 4,711
  • 6
  • 32
  • 93