Saturday, November 17, 2007

Concurrency on multiple core CPUs

As you are well aware, CPU manufacturers are in a race to create multi core CPUs, since they found that increasing clock speed has it limits. So now Intel aims on releasing 32 core CPUs two years from now.

So, how does this affect popular platforms such as Java and .NET? Well, they both use a shared memory model to support multithreading. Every time you want to acquire a lock you need to stop all threads. This is an extremely bad thing to do on multi core machines - you're waisting a lot of resources.
I guess if you're writing desktop applications this may not be so bad since you will probably be able to rely on the OS to do its magic and run each process (yours being one of them) on a different core. But what if you're developing a website or an enterprise application that will probably have it's own dedicated server? In such a case it becomes your responsibility. So what can you do?

Languages such as Erlang and Haskel are functional languages that use message passing instead of shared memory (sometimes called "shared nothing") - with message passing you don't have a shared memory - so no locks are needed and you're free to scale to as many processors as you'd like.


Now, in my opinion, you have three options:
1. Start learning Erlang or Haskell, which are fundamentally different from what you're used to if you're a Java/C# programmer.
2. Hope Sun and Microsoft rewrite their VM's to support message passing effectively (I highly doubt this would ever happen). You could do message passing even today but since the VM is not designed for that - you will get horrible performance.
3. You can be lazy and wait but there's a chance you will write really shitty software 3 years from now.


There's a good talk with Joe Armstrong, inventor of the Erlang programming language, where he explains the fundamental difference between message passing and shared memory:
http://channel9.msdn.com/ShowPost.aspx?PostID=351659

2 comments:

Elad said...

Good post Tal, but what's your take on this - did you start learning Erlang or Haskell, or are you being lazy?

Tal said...

The first step of solving a problem is admitting you have one - so I think I'm in a good direction :-)