Using the Parallel Programming Library in the real world: Futures
Futures are a great tool in the tool chest of every programmer. But, wait! What's a future?
Well, while a task can be seen as a sort of asynchronous procedure, a future can be seen as an asynchronous function; however, while using a task, the process is quite clear (it runs in the background and uses some sort of messaging to talk to the other thread), the future is a bit more complex. When should I get the return value of the future? Let's talk about futures with an example. You can use futures to run tasks on a separate thread and then forget about them, but often, you'll want to use the result of the task. The future function returns an IFuture<T>
reference that you can use to request the result of type T
. The reference is like the ticket that a dry cleaner gives you; at any time, you can use it to request your clean dress, but if your dress isn't clean yet, you'll have to wait. Similarly, you can use the reference...