On Clojure (and concurrency in general)
I just finished watching Clojure Concurrency, a presentation given by the ever-talented Rich Hickey. It’s almost 2 hours, so don’t just watch it if you’re bored.
Now Rich is a really smart guy. He does a talk on this language he wrote called Clojure (pronounced ‘closure’), which is a Lisp-1 running on top of the JVM. I had previously spotted this some time ago on proggit, but it was still in a very early stage (pre-alpha), and I was more infatuated with Erlang at the moment, while reading Armstrong’s Programming Erlang: Software for a Concurrent World.
Concurrency is a very hot topic these days. The big-shots say it’s a new paradigm, and I’m with them on that one. Well, sorta. In his talk, Rich says at one point that “we’ve been doing it wrong.” If you ever tried creating a multithreaded application, ending up in a living hell acquiring locks and having more mutex’es than normal variables, you know the problem at hand.
The “new” proposed way of doing things, tie in directly with the also-hyped functional languages (I’m sure you’ve seen Haskell or F# mentioned, no?) — the gist of this being immutability. That’s a big word that just means “you can’t change it.” Once you’ve assigned a value to something, you can’t change it. There’s a section in the aforementioned video that talks about “Persistent Datastructures” which offers some great insight on how this works beyond simple strings or integers (but on stuff like vectors or hashes.) Once you take the mutable part out of the equation, there’s no need to lock anything. You still need transactions every once in a while, but we’ve been using those in databases for years, so they’re not so scary anymore.
Clojure does not yet have a “platform” for seamlessly distributing computing over a network (only via threads running on the CPU, running on your OS, like Haskell), unlike Erlang who offers “green threads”—that is, threads running in the VM, and not on the operating system. This allows for creating and communicating between threads while keeping it extremely cheap. While it may be argued that distributed computing over a network right in the VM is pretty cool, it might not be a very good indicator when choosing your next programming language.
If you want to read more about concurrency and the languages prevailing on the subject these days, I suggest you read Concurrency (computer science) - Wikipedia, the free encyclopedia) which debates the infamous “Dining Philosophers” problem, and definitely Joe Armstrong’s essay “Concurrency is easy”.
I assume Clojure uses whatever the JVM uses… JVMs used to have green threads, but since green threads have some limitations (they cannot run on separate processors, for instance), they ditched it in favour of native threads. See http://en.wikipedia.org/wiki/Green_threads for more info, green threads do have some benefits as well.
Heh, I actually see now that according to wikipedia, Erlang has threads similar to Java’s original green threads.
Of course, if you hit a barrier with Erlang’s threads, you can just start a new Erlang process.