Does immutability entirely eliminate the need for locks in multi-processor programming?
Posted
by
GlenPeterson
on Programmers
See other posts from Programmers
or by GlenPeterson
Published on 2012-10-24T18:29:00Z
Indexed on
2012/10/24
23:15 UTC
Read the original article
Hit count: 297
Part 1
Clearly Immutability minimizes the need for locks in multi-processor programming, but does it eliminate that need, or are there instances where immutability alone is not enough? It seems to me that you can only defer processing and encapsulate state so long before most programs have to actually DO something.
If a program performs actions on multiple processors, something needs to collect and aggregate the results. All this involves multi-process communication before, after, and possibly during some transformations. The start and end state of the machines are different. Can this always be done with no locks just by throwing out each object and creating a new one instead of changing the original (a crude view of immutability)? What cases still require locking?
I'm interested in both the theoretical/academic answer and the practical/real-world answer. I know a lot of functional programmers like to talk about "no side effect" but in the "real world" everything has a side effect. Every processor click takes time and electricity and machine resources away from other processes. So I understand that there may be more than one perspective to answer this question from.
If immutability is safe, given certain bounds or assumptions, I want to know what the borders of the "safety zone" are exactly. Some examples of possible boundaries:
- I/O
- Exceptions/errors
- Interfaces with programs written in other languages
- Interfaces with other machines (physical, virtual, or theoretical)
Special thanks to @JimmaHoffa for his comment which started this question!
Part 2
Multi-processor programming is often used as an optimization technique - to make some code run faster. When is it faster to use locks vs. immutable objects?
Given the limits set out in Amdahl's Law, when can you achieve better over-all performance (with or without the garbage collector taken into account) with immutable objects vs. locking mutable ones?
Summary
I'm combining these two questions into one to try to get at where the bounding box is for Immutability as a solution to threading problems.
© Programmers or respective owner