Functional Concurrency in .NET
With examples in C# and F#
Riccardo Terrell
  • MEAP began December 2016
  • Publication in Fall 2017 (estimated)
  • ISBN 9781617292996
  • 350 pages (estimated)
  • printed in black & white

The multicore processor revolution has begun. Parallel computation is powerful and increasingly accessible and multicore computation is incorporated into all sorts of applications, including finance software, video games, web applications, machine-learning, and market analysis. To get the best performance, your application has to partition and divide processing to take full advantage of multicore processors. Functional languages help developers support concurrency by encouraging immutable data structures that can be passed between threads without having to worry about a shared state, all while avoiding side effects.

Functional Concurrency in .NET teaches you how to build concurrent and scalable programs in .NET using the functional paradigm. This intermediate-level guide is aimed at developers, architects, and passionate computer programmers who are interested in writing code with improved speed and effectiveness by adopting a declarative and pain-free programming style. You'll start by learning the foundations of concurrency and important functional techniques and paradigms used in the rest of the book. Then you'll dive in to concurrent and parallel programming designs, emphasizing the functional paradigm with both theory and practice with lots of code samples. The third part of the book covers a real "cradle to grave" application implementation, covering the techniques and skills learned during the book.

Table of Contents detailed table of contents

Part 1: Functional Concurrent programming Concepts

1. Functional Concurrency Foundations

1.1. Let's start with terminology

1.1.1. Sequential programming

1.1.2. Parallelism

1.1.3. Multitasking

1.1.4. Multithreading

1.2. Why the need for concurrency?

1.2.1. Present and future of concurrent programming

1.3. The Pitfalls of Concurrent Programming

1.3.1. Concurrency hazards

1.3.2. The sharing-of-state evolution

1.3.3. A simple real-world example: parallel quicksort

1.3.4. Benchmarking in F#

1.4. Why Choose Functional Programming for Concurrency

1.4.1. Benefits of functional programming

1.5. Embracing the functional paradigm

1.6. Why use F# and C# for functional concurrent programming

1.7. Summary

2. Functional Programming Techniques for Concurrency

2.1. Function composition

2.1.1. Function composition in C#

2.1.2. Function composition in F#

2.2. Closure

2.2.1. Captured variables in closures with lambda expressions

2.2.2. Closure in a multithreading environment

2.3. Memoization-caching technique

2.3.1. Memoized web crawler

2.3.2. Lazy memoization for better performance

2.3.3. Gotchas for function memoization

2.4. Effective Concurrent Speculation

2.4.1. Precomputation with natural functional support

2.4.2. Let the best computation win

2.5. Being lazy is a good thing

2.5.1. Strict languages for better concurrency

2.5.2. Lazy caching technique and thread-safe singleton pattern

2.5.3. Lazy support in F#

2.5.4. Lazy and Task, a powerful combination

2.6. Summary

3. Functional Data Structures and Immutability

3.1. Real-world example - Hunt the thread-unsafe object

3.1.1. .NET immutable collections: a safe solution

3.1.2. The .NET concurrent collections: a faster solution

3.1.3. The Agent message passing pattern - a faster and better solution

3.2. Functional data structure (FDS)

3.3. Immutability for a change

3.3.1. Functional data structure for data parallelism

3.3.2. Performance implication

3.3.3. Immutability in C#

3.3.4. Immutability in F#

3.3.5. Functional lists

3.3.6. Building a persistent data structure - an immutable binary tree (B-Tree)

3.4. Recursive function

3.4.1. The tail of a correct recursive function - Tail-Call optimized

3.4.2. Continuation passing style (CPS)

3.5. Summary

Part 2 How to approach different parts of a concurrent program

4. The Basics of Processing Big Data: Data Parallelism Part 1

4.1. What is data parallelism

4.1.1. Data and task parallelism

4.1.2. The "embarrassingly parallel" concept

4.1.3. Data parallelism support in .NET

4.2. The Fork/Join pattern: Parallel Mandelbrot

4.2.1. The downside of parallel loops

4.2.2. Amdahl's Law

4.2.3. Gustafson's Law

4.2.4. The limitations of parallel loops: the sum of prime numbers

4.2.5. What can possibly go wrong with a simple loop?

4.2.6. The declarative parallel programming model

4.3. Summary

5. PLINQ and Map-Reduce: Data Parallelism Part 2

5.1. A short introduction to PLINQ

5.1.1. How is PLINQ more functional?

5.1.2. PLINQ and pure functions: the parallel words counter

5.1.3. Isolate and control side effects: fixing the parallel words counter

5.2. Aggregating and reducing data in parallel

5.2.1. Deforesting: one of many advantages to folding

5.2.2. Fold in PLINQ: the Aggregate functions

5.2.3. Implementing a parallel Reduce function for PLINQ

5.2.4. Parallel list comprehension in F#: PSeq

5.2.5. Parallel array in F#

5.3. Parallel MapReduce pattern

5.3.1. The Map and Reduce functions

5.4. Summary

6. Real-Time Event Streams: Functional Reactive Programming (FRP)

6.1. What is Reactive programming: Big Event processing

6.2. .NET tools for Reactive programming

6.2.1. Event combinators - a better solution

6.2.2. .NET interoperability with F# combinators

6.3. Reactive programming in .NET: Reactive Extensions (Rx)

6.3.1. From LINQ/PLINQ to Reactive Extensions

6.3.2. IObservable - the dual IEnumerable

6.3.3. Reactive Extensions in action

6.3.4. Real-time streaming with Reactive Extensions

6.3.5. From events to F# observables

6.4. Taming the event stream - Twitter emotion analysis using Rx programming

6.5. An Rx publisher - subscriber

6.5.1. The Subject type

6.5.2. Rx in relation to concurrency

6.5.3. Implementing a reusable Rx Publisher-Subscriber

6.5.4. Analyzing tweet emotions using an Rx Pub-Sub class

6.5.5. Observer in action

6.5.6. The convenient F# object expression

6.6. Summary

7. Task-based Functional Parallelism

8. Asynchronicity for the Win

9. Asynchronous Functional Programming

10. Functional Combinators and Interoperability

11. Applying Reactive Programming Everywhere With Agents

Part 3: Building Your Toolbox for Success

12. Debugging Functional Error Handling in concurrent operation

13. Useful Concurrent Design Patterns

14. Reactive & Responsive UI (Mobile Development)

15. Scalable Server Side Programming


Appendix A: Functional Programming Fundamentals

Appendix B: F# Overview

What's inside

  • Code examples in both C# and F#
  • Building high-performance concurrent systems
  • Integrating concurrent programming abstractions
  • Concurrent patterns such as fork/join, producer-consumer, Map-Reduce and pipeline
  • Implementing a real-time event stream processing
  • Seamlessly accelerate sequential programs by using data-parallel collections
  • Creating a data-access layer to handle massive concurrent requests

About the reader

This book is for readers with solid knowledge of a mainstream programming language, preferably C# or F#.

About the author

Riccardo Terrell is a .NET seasoned software engineer, senior software architect and Microsoft MVP who is passionate about functional programming. He is well known and actively involved in the functional programming community including .NET meet ups and conferences and is the organizer for the Washington DC F# User Group.

Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
MEAP combo $59.99 pBook + eBook
MEAP eBook $47.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks