Chapter 9. Asynchronous and Parallel Programming

“Parallel programming” frightens many developers, as the term has become synonymous with complexity and difficult-to-find bugs. However, with the libraries and language features available to F#, any anxiety over parallel programming is misplaced.

In fact, even if parallel programming were difficult—which I contend it is not—it is still worth studying because without it you simply cannot take advantage of your hardware’s potential.

This chapter focuses on how to speed up computation in F# using asynchronous and parallel programming. By the end of this chapter, you will be able to execute code in different contexts (threads), use the F# asynchronous workflows library for mastering asynchronous programming, and also take advantage of the Parallel Extensions to .NET.

Before we begin, let’s define some of the domains related to “parallel programming”:

Asynchronous programming

Asynchronous programming describes programs and operations that once started are executed in the background and terminate at some “later time.” For example, in most email clients, new emails are retrieved asynchronously in the background so the user doesn’t have to force checking for new mail.

Parallel programming

Parallel programming is dividing up work between processing resources in order to speed up execution. For example, converting a song into an MP3 can be parallelized—dividing the song into pieces and converting each segment in parallel.

Reactive programming

Reactive ...

Get Programming F# 3.0, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.