Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is lockless concurrency such a big deal (in Clojure)?

I'm told that Clojure has lockless concurrency and that this is Important.

I've used a number of languages but didn't realize they were performing locks behind the scenes.

Why is this an advantage in Clojure (or in any language that has this feature)?

like image 468
i_like_monkeys Avatar asked Sep 01 '09 05:09

i_like_monkeys


3 Answers

Lockless concurrency also provides the nice advantage that readers never have to wait for other readers. This is especially useful when many threads will be reading data from a single source. You still need to define the data dependencies in your program and explicitly define the parts of a transaction that can be commuted safely.
STM saves you from deadlocks and almost all occurrences of livelock though it does not save you from concurrency failures you can still create cases where a transaction will fail because it lacks the resources to maintain its history but the important part is that concurrency failures will be explicit and you can recover from them

like image 155
Arthur Ulfeldt Avatar answered Sep 27 '22 17:09

Arthur Ulfeldt


I can't speak about Clojure specifically, but ... it means you don't need to wait for someone to be done with something before you can get to work. Which is great.

Typically it's achieved with immutable types. If nothing can be modified, you don't really need to wait till someone else is done with it before you can access it.

like image 32
Noon Silk Avatar answered Sep 27 '22 17:09

Noon Silk


Deadlocks. Or to be more correct the lack of them.

One of the biggest problems in most languages is that you end up with deadlocks that are:

  1. Hell on earth to debug.
  2. Difficult to be sure you have gotten rid.

Now with no locks, obviously you won't run into deadlocks.

like image 36
tomjen Avatar answered Sep 27 '22 16:09

tomjen