Replies: 2 comments
-
Third code block has |
Beta Was this translation helpful? Give feedback.
0 replies
-
Fixed.
It can be, although it's not as nice. I wrote this prior to |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Copied from dotnet/roslyn#1165
Background
In my opinion one of the more interesting ideas in the Polyphonic C# and Cω languages out of Microsoft Research was "asynchronous method chords". Combined with asynchronous methods this simple syntax provided one of the most elegant ways to express complex synchronization between two or more asynchronous operations.
Proposal
An asynchronous pattern is comprised of two parts.
Mailbox Methods
The first part is an asynchronous method which has no body and must return
void
. This method serves as a notification primitive. Invoking the method is the same as posting a message where the arguments are additional pieces of data transmitted with the message.When invoked a mailbox method effectively returns immediately to the caller and can never throw exceptions.
Pattern Methods
The second part is an asynchronous pattern method which may return
void
,Task
orTask<T>
and has a normal method body. This method is decorated with one or more&
join operators with the name and signature of a mailbox method between the parameter list and the method body. The parameter names in the joined mailbox signature may be renamed. The values of those parameters are available in the body of the pattern method.After the pattern method body more join operations may appear using different permutations of the available mailbox methods. These permutations must be unique per each method body regardless of order. When the pattern method is invoked which body is executed depends on which mailbox methods are invoked.
What happens when the pattern method depends on whether there have already been corresponding calls to the mailbox methods. For a pattern method body to be executed there must be corresponding calls to every mailbox method on which it joins.
If there are no corresponding calls to the mailbox methods for any of the pattern methods then the method returns immediately. Once a mailbox method is called which satisfies the pattern method then that pattern method body will be executed on the same thread that invoked the mailbox method.
If the method returns
Task
orTask<T>
the returned task is incomplete. If there are corresponding calls to the signal methods for a pattern method then the method is invoked on the current thread.One the pattern method body is executing it behaves like a normal asynchronous method. It may use the
await
keyword to await on other asynchronous operations, including other pattern methods. It may return a value which sets the result on the returnedTask
orTask<T>
instance, or it may throw an exception which sets the exception on the returnedTask
orTask<T>
instance.Reference material:
Cω Overview
Cω Concurrency Examples Tutorial
Cω Concurrency Specification
Note that I am only using the Cω as a reference. The syntax that I proposed is different in that it builds on
async/await
from C# 5.0. From a functionality point of view the biggest difference is that the pattern method is not synchronous, as it is in Cω. If the pattern method has no corresponding signal method invocations it will not block but rather return an incompleteTask
.Beta Was this translation helpful? Give feedback.
All reactions