Cω - Samples

Concurrency Extensions Tutorials

This section contains tutorials for understanding how to use the extensions for asynchronous programming in the Cω programming language.

Cω extends the C# programming language with new asynchronous concurrency abstractions. The language presents a simple and powerful model of concurrency that is applicable both to multithreaded applications running on a single machine and to the orchestration of asynchronous, event-based applications communicating over a wide area network.

The new constructs are a mild syntactic variant of those we have previously described under the name "Polyphonic C# "-- Cω combines Polyphonic C# with the rich new data programming model described from X#/Xen.

The Basic Idea

In Cω, methods can be defined as either synchronous or asynchronous. When a synchronous method is called, the caller is blocked until the method returns, as is normal in C#. However, when an asynchronous method is called, there is no result and the caller proceeds immediately without being blocked. Thus from the caller's point of view, an asynchronous method is like a void one, but with the useful extra guarantee of returning immediately. We often refer to asynchronous methods as messages, as they are a one-way communication from caller to receiver (think of posting a letter rather as opposed to asking a question and waiting for an answer during a face-to-face conversation).

By themselves, asynchronous method declarations are not particularly novel. Indeed, .NET already has a widely-used set of library classes which allow any method to be invoked asynchronously (though note that in this standard pattern it is the caller who decides to invoke a method asynchronously, whereas in Cω it is the callee (defining) side which declares a particular method to be asynchronous). The significant innovation in Cω is the way in which method bodies are defined.

In most languages, including C#, methods in the signature of a class are in bijective correspondence with the code of their implementations -- for each method which is declared, there is a single, distinct definition of what happens when that method is called. In Cω, however, a body may be associated with a set of (synchronous and/or asynchronous) methods. We call such a definition a chord, and a particular method may appear in the header of several chords. The body of a chord can only execute once all the methods in its header have been called. Thus, when a polyphonic method is called there may be zero, one, or more chords which are enabled:

In This Section

Buffer Tutorial
Provides a simple demonstration defining an unbounded buffer using Cω chords.
One-Place Buffer Tutorial
Provides a simple demonstration of a one-place buffer using Cω chords.
Spawning Threads Tutorial
Provides a simple demonstration of spawning threads using Cω asynchronous methods.
Using Asynchronous Messages For State Tutorial
Provides a simple demonstration of using asynchronous messages to carry state across method calls.
Reader-Writer Lock Tutorial
Provides a simple demonstration of how program a Reader/Writer-lock using Cω.
Bounded Buffer Tutorial
Provides a simple example of defining a bounded buffer using Cω chords.

The previous samples described how private asynchronous messages may be used to carry state. The following samples show how to orchestrate asynchronous messages between different objects:

Semaphores Tutorial
Provides a simple demonstration of how to encode semaphores.
Asynchronous Call and Return Patterns Tutorial
Provides a simple demonstration of how to return values from asynchronous calls.
Active Objects Tutorial
Provides a simple demonstration of how to define active objects (agents).

See Also

Sample source code showing how to write applications for the .NET Framework using Cω.