Saturday, August 16, 2008

Threads Are Evil

I never refuse to express my opinion about threads: they are Evil.

Using threads is like selling your soul to Satan without even noticing it.
They offer you the paradise with concurrency and shared memory for your programs.
After the first week using it, you see yourself working full time to correct the "special cases" you weren't aware of. After one month you become a slave of your own program.

The other (not so) common approach to concurrency is message passing.
It seems that it is always gaining popularity, but its use is still restricted to niches. It was never really adopted by a mainstream language.
Message passing eradicates memory corruption and is seen as a safer model.

What is common with these models is that they are both asynchronous and non-deterministic.
By asynchrony I mean that the concurrent entities run unaware of each other. If they want to synchronize, they must use a language primitive for it (receive, lock, synchronized, etc).
The non-determinism is also inherent here, one can never be sure when the scheduler will preempt a running entity. This is, of course, a source of non-deterministic bugs.

What is the point of using asynchronous languages to write synchronous programs?
We feel (probably we learnt) that we have no option: If we want to synchronize code we must do it explicitly.
That is so not true as I'll try to defend here...

3 comments:

  1. I agree. Threads are evil. I read somewhere a interesting paragraph about this subject (IIRC was written by Allan Cox):

    "Threads are the best way to shoot your both feet at the same time."

    ReplyDelete
  2. "By asynchrony I mean that the concurrent entities run unaware of each other..."

    acho que estão aware sim, tanto estão, que ... "If they want to synchronize they must use a language primitive for ..."

    Pode ser que elas não saibam o que as outras estão fazendo a cada momento, mas que estão aware, estão.

    G.

    ReplyDelete
  3. The end of your post was something like: "Don't miss the next chapters" :-)

    It seems the other two comments were more useful :-D

    ReplyDelete