Living with Latency
Our customers demand more speed and responsiveness from applications. At the same time, as the applications are becoming more and more connected, we are increasingly confronted with the latency inherent in I/O devices.
A thread performing a blocking I/O operation effectively delegates control to the hardware device performing that operation. Depending on the nature of the operation, and the ambient conditions such as the network throughput, the operation may complete right away or after a considerable delay. Because such conditions are unpredictable, an application that is fast and responsive when run on the developer’s desktop machine can be infuriatingly slow on the customer’s laptop or on a server in a datacenter.
A technique well-known to web developers – AJAX – relies on issuing multiple asynchronous requests, each associated with its own completion event, as in the example below:
The key to avoiding a blocked thread is to divorce the start of the operation from its completion. The main thread can then carry on performing other tasks with the completion fired at a later time.
This approach has also been used successfully in .NET, where it is known as the Event-based Asynchronous Pattern.
With Windows 8, you can write Metro style apps using the Windows Runtime asynchronous operations, which are built on the same principle.
Introducing Windows Runtime Asynchronous Operations
The Windows Runtime allows developers to build responsive applications using the new asynchronous programming model that is based on the idea of separating the start of the operation from its completion.
First, we’ll look at an example of how you program against the Windows Runtime directly to create a file in the standard “Pictures” folder. You will notice a few new keywords like ref new that are part of the C++ language extensions Microsoft introduced to support Windows Runtime. There is a lot more to say on this, but that’s not the subject of this post – so if you’re hungry for information, I recommend this Channel9 talk by Herb Sutter delivered recently at the //BUILD/ conference, or this or this in-depth talks from our C++ compiler experts.
In the meantime, here goes the example:
The delegate for the completion handler takes a value of type IAsyncOperation<StorageFile^>^. Here we still don’t have a handle to the StorageFile – what we have is again an operation (completed or failed) from which we can get the StorageFile^ by calling a non-blocking method GetResults.
Finally, we must start the operation by calling the Start method.
In a non-trivial application multiple asynchronous operations are usually composed together. For example, writing a string into file requires chaining together four asynchronous operations, as in the following example:
The example above assumes that none of the asynchronous operations can fail or be cancelled, which is an unrealistic expectation for a real-world application. When an error does occur, it can be handled in two ways. First, you can put the call to GetResults in a try block – when the operation fails, the call to GetResults will throw an exception. Second, you can check the status of the operation prior to calling GetResults:
In general, the error handling is needed for every asynchronous operation that can fail.
Windows Asynchrony with PPL
Using asynchronous operations is essential for building responsive and scalable applications, but it requires a new programming model and a new API that are frankly, harder to master than the traditional synchronous model.
We in the Concurrency Runtime Team pride ourselves in improving the experience of C++ developers writing concurrent code. As it turns out, the new model for asynchrony in Windows 8 maps pretty well to a set of concepts we introduced earlier this year, the PPL Tasks.
The concept of the asynchronous operation represents an “eventual” result and thus can be easily mapped to the concept of the task. Setting a completion event (the Completed property above) is conceptually similar to scheduling a continuation on the task.
Not only do we have a good mapping between the concepts, PPL Tasks significantly reduce the amount of boiler-plate code that needs to be written and compose well with each other.
Without further ado, let’s revisit the example of writing a string into a file and rewrite it using PPL Tasks:
The constructor of the task accepts a parameter of type IAsyncOperation<T>^, resulting in a task<T>. The continuation of that task – i.e. the lambda passed into the then method – executes when the operation used to instantiate the task completes. As you already know from our last post, the then method returns a task, allowing you to “chain up” multiple methods together.
For operations that can fail, you can create a continuation where the resulting value is wrapped into a task. To get a value out of the task, the user can call get – which will throw if the underlying asynchronous operation resulted in a failure:
Fortunately, when you’re chaining together a list of continuations, not every continuation needs to handle errors. When an exception occurs in any of the tasks of the chain, the exception propagates “down” to the first error-handling continuation in the chain. This way only the last continuation of the chain needs to handle errors.
Hence we can re-write our PPL example above as follows:
We think that PPL will make using Windows 8 asynchronous operations significantly simpler, but we want to hear your feedback. Do you find it easy to use? Would you use Windows 8 asynchronous operations with or without PPL?
We’ve added a few more advanced samples to our sample pack – go ahead and download it, experiment with the samples and let us know what you think!
Concurrency Runtime Team