Rich Hickey: STMs vs Locks [2008]
azulsystems.com
azulsystems.com
The reason I don't understand this argument is that I was always under the impression that you shouldn't normally DO very much at all in a transaction. As in, most transactions should basically just consist of a single pointer-swap.
What are typical operations during transactions that are more complex than this? I suppose if several variables are modified in sequence there could be problems---is this typical when STM is used in object-oriented languages?
If so, that basically demonstrates the value of using immutable data structures, generally considered beneficial for any form of concurrency. And so it's no wonder that STM is more popular in functional languages.
But when I've used locks in the past in C++ or Java, I've always avoided problems by reducing my synchronization periods to pointer swaps, either for updating a state vector or for passing ownership of an object; so the same lesson, I believe, is applicable to locks. Which leads me to believe that STM simply enforces practices that are good when using locks anyways, and additionally simplifies their implementation.
Rich is right about correctness. With an STM, it's comparatively easy to get correct behaviour from your program, which is more important than performance.
But, I still agree with you because even then, I would try to keep the amount of data which needs to be atomically shared to a minimum, so the problems discussed would be minimal.
Also, as you mentioned, with immutable data, even STM touching multiple complex data structures is simplified because you can create the new versions independently and then simply swap a pointer or two, so in funtional languages with immutable data, I expect STM to be turned into a few atomic pointer swaps under the hood.
FWIW, this was implemented in Clojure soon after this conversation; see atoms.
Yeah I think it is a design issue. STMs are just a better abstraction. But the same critical data structure is pounded from thousands of concurrent readers and writers it will be slow, no matter abstraction is used.
There are cases where that is hard to avoid but often is should call for a redesign.
As the number of cores increases or "cloud" computing becomes more mainstream, at some point shared updatable state will be the exception rather than the default.
In the long term I think something like Erlang's actors & distributed computation model will become more popular. Maybe it will not be Erlang, maybe Go. But something like "Pid ! Msg" where Pid is in the same OS process, different OS process or even different process on a different host half way across the world, will become a new way to think about programming.
That's why I started learning more about ZeroMQ http://www.zeromq.org and structured my latest work project as multiple processes instead of multiple threads. Each process is single-threaded, and (at least so far) relatively easy to reason about.
With Erlang, which was perhaps confusing from my post, the Pid identifies an "Erlang process". It is a very light weight unit of execution. One can have tens of thousands of them in a single OS process (which they call a node).
However, if you don't need tens of thousands of processes, there is not reason why one couldn't structure their app using real OS processes and using 0QM.
It is a well written text on building reliable distributed systems in general, and Erlang in particular.
STM or Actors can both be useful as concurrency and distributed programmes but non is a silver bullet in either or both of them.
Pid ! Msg.
And here is how to send a message to a Pid located on some server farm in Japan: Pid ! Msg.
In general CPUs are probably not going to get faster. Networks might, both on LANs and WAN. There is the 100Gb Ethernet, optical interconnects I think will become cheaper.On the motherboard # of cores will be increases but at some point, while we get the hang of concurrency and restructure our codebase, we'll quickly get hungry for more cores and memory than a single server can support.
What happens then Erlang program won't have to be restructured when that happens because it provides a suitable abstraction. At the lowest level it will still be Pid ! Msg. That is the beauty of it.
Erlang's popularity for concurrent programming probably comes down to the fact that anything is better than manually managed locks and until recently there weren't any widely-used industrial languages that used synchronous message passing or transactional memory. I don't think it's any coincidence, though, that the designers of more recent concurrent languages like Go and Clojure have not followed the Erlang model.
http://www.reddit.com/r/programming/comments/fgktt/transacti...
this haskell guy didn't get the memo, 80k variables! tldr, ghc in 2008 was pretty amazing
http://www.azulsystems.com/blog/cliff/2010-06-04-or-how-i-go...