by seanmcdirmid · 9y ago · 0 comments
What we are talking about here are immutable data structures that replace mutable ones, where mutability is pushed off to a higher reference cell (so instead of having a mutable set, we have a mutable variable that points to a new immutable version of the set). In both cases, locking is required if thread safety is desired.
This is not at all how functional data structures work. You're assuming that the data structures are the same as non-functional data structures, but merely without the ability to mutate them. That is not correct. Proper functional data structures are implemented completely differently.
One obvious exception to this is the Swift language, which is not really a functional programming language, and it does not have functional data structures. It uses copy-on-write which is somewhat similar to what you describe, but is not how functional programming languages do it.
https://hackage.haskell.org/package/base-4.10.0.0/docs/Contr...
>An MVar t is mutable location that is either empty or contains a value of type t.
>They can be used in multiple different ways:
>1. As synchronized mutable variables,
There are some performance implications with NUMA, but there aren't serious multi-threading problems with immutable structures. Immutability means there isn't anything to race with, as long as initial construction is retired properly before other threads see anything, which is a compiler / runtime implementation problem, not an end user programmer problem.
Now, you might argue such needs ought not arise. In practice, due to resource constraints, however, they often do.