Asynchronous processing
Executing asynchronously involves forking a computation to execute in another thread and right after continuing to do other things. In interactive applications, it is often useful to execute things in the background, in order not to block the user interface for too long. Usually we want to use the results from the asynchronous worker thread once it has finished. Sometimes we wish to cancel a long-running asynchronous computation, in order not to leave unwanted jobs lying around.
Although it is totally possible to create asynchronous jobs with waits and cancels using MVar
and perhaps STM
, the Async API in the async
package provides much nicer solutions.
But first, to be convinced there's nothing magical in the Async API abstraction, we'll build something with just MVar
and STM
. So, forking multiple asynchronous threads and waiting for their results is trivial with a few instances of MVar
: just create an MVar
for every worker and make the workers put their results into...