Improving the Asynchronous Example
To wait for the next redeemable IOU, use the RWTIOUTrap class. It uses an internal producer-consumer queue to capture RWTIOUResult instances as they become redeemable. To trap an IOU result, the code must pass the IOU handle to the RWTIOUTrap instance in a call to the trap’s setTrap() function. This approach is an improvement on the way asynchronous operations were handled in Example 46.
Once an IOU has been registered with a trap, a handle to the IOU is automatically enqueued inside the trap when the IOU is closed. An IOU can be registered with any number of traps—a separate handle is enqueued in each trap instance at IOU closure.
If the IOU has already been closed and is redeemable when the IOU is added to a trap, a redeemable IOU is enqueued immediately during the setTrap() call.
A thread waits for trapped IOUs to become redeemable by calling the trap’s getNext() function. This function returns the next IOU in its internal queue and waits for an IOU to arrive if the queue is empty.
See Trapping IOUs with RWTIOUTrap for additional information regarding this class.