Using IOUs Example
The following example demonstrates the use of an IOU object for retrieving the results of an operation executed in another thread. Example 45 uses a form of threaded runnable that invokes a function and stores any return value into an IOU.
Example 45 – Using an IOU object to retrieve results from another thread
#include <rw/thread/RWRunnableSelf.h>
#include <rw/thread/RWTThreadIOUFunction.h>
#include <rw/itc/RWTIOUResult.h>
#include <iostream>
 
using namespace std;
 
int sync_service()
{
int status = 0;
// Do something useful that produces a status
// value indicating success or failure
rwSleep(1000); // Waste some time...
return status;
}
RWTIOUResult<int> async_service()
{
RWTThreadIOUFunction<int> thread;
thread = RWTThreadIOUFunction<int>::make(sync_service); // 1
thread.start(); // 2
return thread.result(); // 3
}
void main()
{
cout << "Starting asynchronous service..." << endl;
RWTIOUResult<int> iou = async_service(); // 4
cout << "Redeemed value of IOU: " << iou << endl; // 5
}
//1 Creates a threaded runnable to execute the service.
//2 Starts the thread.
//3 Returns the IOU result for the service.
//4 Invokes the asynchronous service.
//5 Redeems the IOU.