Legend

Class
Struct
Enum
Interface
Delegate
Constructor
Method
Property
Event
Field

Sealed class: RT.KitchenSink.Channel<T>

Summary

Provides a synchronized channel for communication between concurrent threads.

Generic type parameters

T The type of message exchanged between threads.

Remarks

The design of this class is based on the concept of channels in the Go programming language:

  • The basic principle is to have one thread “write” (enqueue) items and another “read” (dequeue) them. The writing thread can signal the end of the channel by closing it.
  • Any number of threads can write to a channel. However, a master thread must be responsible for closing the channel; if nobody closes it, reads will block indefinitely, and if a thread attempts to write after another thread closed the channel, an exception occurs.
  • Any number of threads can read from a channel. If no item is waiting in the queue, the reading thread blocks until another thread either writes an item or closes the channel.
  • A closed channel can still be read from until all the enqueued elements are exhausted, at which point reading from the closed channel throws. The Channel<T>.TryRead(out T) method can be used to avoid this exception. Note that accessing Channel<T>.HasMore is not enough as another reading thread can dequeue an item at any time.
  • The easiest way to read from a channel safely is to iterate over it using a foreach loop. Multiple threads can use such a loop simultaneously; doing so will “spread” the items across the threads. The foreach loops all end when the channel is closed and all elements are exhausted.

Constructors

Creates a new instance of Channel<T>.

Instance methods

void Signals the end of the channel. Threads can still read elements waiting in the channel, but no further elements can be written to it.
IEnumerator<T>
  • Implements: IEnumerable<T>.GetEnumerator()
Returns an enumerator that allows safe reading from this channel. Multiple threads can call this to iterate over the channel; the items from the channel are spread across those threads. (see also remarks)
T Reads an element from the channel. If no element is waiting to be read, blocks until an element is received or the channel is closed. (see also remarks)
bool
TryRead(out T result)
Determines whether an element can be read from the channel and if so, reads it. If no element is waiting to be read, blocks until an element is received or the channel is closed. (see also remarks)
void
Write(T element)
Writes an element to the channel. (see also remarks)

Instance properties

bool Determines whether the channel contains more items. (see also remarks)