0

It's usually said that immutable data structures are more "friendly" for concurrency programming. The explanation is that if a data structure is mutable and one thread modifies it, then another thread will "see" the previous mode of the data structure.

Although it's impossible to modify immutable data structure, if one needs to change it, it's possible to create a new data structure and give it the reference of the "old" data structure.

In my opinion, this situation is also not thread safe because one thread can access the old data structure and the second one can access the new data structure. If so, why is immutable data structure considered more thread-safe?

CrazySynthax
  • 13,662
  • 34
  • 99
  • 183
  • "if a data structure is mutable and one thread modifies it, then another thread will 'see' the previous mode of the data structure." This is often not so simple. In all languages I know, the behavior of a unsynchronized write and read is undefined. For example, if the structure is made of multiple bytes, some bytes could be overwritten with the new the data while others still contain the old data, which could result in an invalid state for certain structures – KABoissonneault Aug 14 '16 at 15:29
  • so you actually agree with me that immutability doesn't solve the concurrency problem. – CrazySynthax Aug 14 '16 at 15:32
  • Immutability usually means "no writes allowed". Concurrent unsynchronized reads are usually defined behaviors, while any concurrent unsynchronized read-write or write-write are undefined. What you describe in your question is not a violation of this principle – KABoissonneault Aug 14 '16 at 15:45

1 Answers1

3

The idea here is that you can´t change an object once it has been created. Every object that is part of some structure is itself immutable. When you create new structure, and reuse some components of old structure, you still can´t change any internal value of any component that makes up this new structure. You can easily identify each structure by its root component reference.

Of course, you still need to make sure you swap them in thread safe fashion, this is usually done using variants of CAS (compare and swap) instructions. Or you can use functional programing, which idiom of functions without side-effects (take immutable input and produce new result) is ideal for thread safe, multi threaded programming.

There are many benefits to immutability over mutability, but that does not mean immutable is always better. Every approach has its benefits and applications. Take a look at this answer for more details on immutability uses. Also check this nicely written answer about mutability benefits in some circumstances.

Community
  • 1
  • 1
B.Gen.Jack.O.Neill
  • 8,169
  • 12
  • 51
  • 79
  • If when I create a new data structure and assign it to the old reference, I still have to do it with a "thread safe fashion", what is the benefit of mutable data over immutable data structures? – CrazySynthax Aug 14 '16 at 15:31
  • This is different language to language, but usually you have some kind of factory or builder, which will build new object from provided components. For example Car.getBuilder().addEngine(engine).addChasis(chassis).build(). You will get new Car object only after calling build() method, therefore its creation is thread safe and simple. – B.Gen.Jack.O.Neill Aug 14 '16 at 15:36