concept share mutable state in category functional programming

appears as: shared mutable state, shared mutable state, shared mutable states
Grokking Functional Programming MEAP V11

This is an excerpt from Manning's book Grokking Functional Programming MEAP V11.

What is shared mutable state?

state is one instance of a value that is stored in one place and can be accessed from the code. If this value can be modified, we have a mutable state. Furthermore, if this mutable state can be accessed from different parts of the code, it’s a shared mutable state.

Let’s go through our problematic examples and define which parts could be categorized as shared mutable state and caused us headaches:

Functional Programming in C#: How to write better C# code

This is an excerpt from Manning's book Functional Programming in C#: How to write better C# code.

Doing several things at the same time can really boost performance. It also means that the order of execution isn’t guaranteed, so concurrency can be the source of difficult problems, most notably when multiple tasks concurrently try to update some shared mutable state. (In later chapters, you’ll see how FP addresses this by avoiding shared mutable state altogether.)

Every seasoned developer has some first-hand experience of how difficult it can be to deal with problems such as deadlocks and race conditions. These are the hard problems that can arise in concurrent programs that involve shared mutable state (“shared,” that is, between processes that execute concurrently).

This is why, throughout this book, you’ve seen many examples of how to solve problems without making recourse to shared mutable state. Indeed, my recommendation is to avoid shared mutable state whenever possible, and FP provides an excellent paradigm for doing so.

In this chapter, you’ll see why it’s not always possible to avoid shared mutable state, and what strategies there are to synchronize access to shared mutable state. We’ll then concentrate on one of these strategies: agent-based concurrency, a style of concurrent programming that relies on message-passing between agents that “own” some state that they access in a single-threaded way. Programming with agents is popular with F# programmers, but you’ll see that it’s perfectly doable in C#.

15.1. The need for shared mutable state

It’s generally possible to avoid shared mutable state when designing parallel algorithms. For instance, if you have a computationally intensive problem that you’d like to parallelize, you can usually break the data set or the tasks down in such a way that several threads compute an intermediate result independently. Hence, these threads can do their work independently, without the need to share any state among themselves. Finally, another thread may compute the final result by combining all the intermediate results.

The problem, however, is that avoiding shared mutable state isn’t always possible. Although it can generally be achieved in the case of parallel computations, it’s much more difficult if the source of concurrency is multithreading. For example, imagine a multithreaded application, such as a server application handling requests on multiple threads, that needs to do the following:

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest