Asynchronous Example
In Example 46, four operations are started before attempting to redeem the results. Assume that:
*The requests are performed in parallel.
*Each of the requests can take a varying amount of time to complete.
In this hypothetical situation, the IOU results are likely to become redeemable in an order different from the order in which the corresponding operations were launched.
Example 46 – Responding to results from asynchronous operations in order of launch
ServiceProvider provider;
 
RWTIOUResult<int> iou1 = provider.asyncService();
RWTIOUResult<int> iou2 = provider.asyncService();
RWTIOUResult<int> iou3 = provider.asyncService();
RWTIOUResult<int> iou4 = provider.asyncService();
 
cout << "Redeemed value of IOU1: " << iou1 << endl;
cout << "Redeemed value of IOU2: " << iou2 << endl;
cout << "Redeemed value of IOU3: " << iou3 << endl;
cout << "Redeemed value of IOU4: " << iou4 << endl;
Redeeming the IOUs in the way shown above forces the results to be acted upon in the same order in which the operations were launched. To take maximum advantage of the concurrency of threads, the application needs to be designed to process the results of these operations in the order in which they become available.
Consider a hypothetical Web browser written using the Threading and Interthread Communication packages. A typical Web page can consist of text and images. Each of the images is identified by a Web link and can be distributed across various sites on a network. The browser, after loading a Web page containing these image links, can choose to launch separate threads to handle the retrieval of each image. In this situation, IOUs can represent the individual images being retrieved by each of the threads. If the display code in the browser were forced to redeem the IOUs in the same sequence as the retrieval operations were launched, then a lengthy transfer could unnecessarily delay rendering of the remaining portions of the page. If, however, our hypothetical browser could draw each image as it arrives, the user would see each image on the page appear as soon as it became available.