CKTIS / C#
Channel — asynchronous producer-consumer data transfer
System.Threading.Channels.Channel creates channels connecting producers and consumers. A bounded channel can asynchronously wait for available space, while an unbounded channel imposes no item-count limit. Options also describe guarantees about concurrent read and write operations.
Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification
Syntax
Channel<T> channel = Channel.CreateBounded<T>(new BoundedChannelOptions(capacity) { FullMode = BoundedChannelFullMode.Wait, SingleReader = true, SingleWriter = true });
Channel<T> unbounded = Channel.CreateUnbounded<T>(new UnboundedChannelOptions());System.Threading.Channels.Channel is a static factory class, while Channel<T> exposes Writer and Reader. Channel.CreateBounded<T> and Channel.CreateUnbounded<T> create FIFO queues for asynchronous data transfer. Multiple consumers divide the items among themselves: a channel does not broadcast every item to everyone. The APIs covered here are available in .NET 10; the example requires no preview language features.
BoundedChannelOptions specifies the channel capacity and what happens when the channel cannot accept another item. In .NET 10, the capacity can be positive or 0. A capacity of 0 creates an unbuffered channel: a write and a read must rendezvous to transfer the item directly. In the default BoundedChannelFullMode.Wait mode, if a write cannot proceed immediately, WriteAsync waits asynchronously for buffer space or, on an unbuffered channel, for a matching read. It does not block a thread; TryWrite returns false in the same situation. This provides backpressure that slows the producer. The other full-channel modes can discard items.
UnboundedChannelOptions configures a channel without a fixed capacity limit, created through Channel.CreateUnbounded<T>. Writes do not wait for space, but if the producer continually outpaces the consumer, the buffer can consume increasing amounts of memory. Both option types inherit ChannelOptions.SingleReader and ChannelOptions.SingleWriter. Setting either to true promises at most one read or write operation at a time, enabling optimizations; it is not a lock enforcing that rule. Leave it false without that guarantee.
After writing finishes, call Writer.Complete(): ReadAllAsync() drains the buffer and ends iteration. With multiple producers, complete the channel only after all writers finish. An error can be passed through TryComplete(error), and a cancellation token can interrupt waiting. Run the producer and consumer concurrently; awaiting producer completion before starting reads can deadlock a full channel using Wait.
Examples
using System;
using System.Threading.Channels;
using System.Threading.Tasks;
Channel<int> channel = Channel.CreateBounded<int>(
new BoundedChannelOptions(1)
{
FullMode = BoundedChannelFullMode.Wait,
SingleReader = true,
SingleWriter = true
});
Task producer = ProduceAsync(channel.Writer);
await foreach (int value in channel.Reader.ReadAllAsync())
{
Console.WriteLine(value);
}
await producer;
static async Task ProduceAsync(ChannelWriter<int> writer)
{
try
{
for (int value = 1; value <= 3; value++)
{
await writer.WriteAsync(value);
}
writer.Complete();
}
catch (Exception error)
{
writer.TryComplete(error);
throw;
}
}Result
1 2 3
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.