Why do we need threads?

sky on 2001-06-09T14:19:26

I have recently tried to push along the development of thread support to perl! I started doing this after I realized that perl has pretty good support for running multiple interpreters at the same time! This is mainly work done by Sarathy of ActiveState to support fake forking on windows. However there were was no way to use this functionality from perl.

When I start talking about threads, people tell me that we don't need threads. I either get the response.

  • We don't need threads, go write an event loop for perl
  • We don't need threads, we got POE
  • We got fork and IPC
Lets now deal with each of these statements.



We don't need threads, go write an event loop for perl
Excuse me, I have already done it, and then I helped Rocco write his excellent POE event loop, the most flexible and powerfull event loop in the perl world. People who tell me this and haven't tried POE are not going to get alot of confidence from me.

We don't need threads, we got POE
With heavy work in POE I say that POE is not enough. The problem with POE is the fact that the rest of world is not cooperating. Some libraries, some external resources, some events are blocking. There is nothing in the entire world we can do to make them nonblocking! Also POE only uses one processor even if more are avaible. This is not a very good solution when machine trend is SMP. Therefore POE is going to get ithread support if things go by plan.



We got fork and IPC
Fork and IPC are not replacments for threads. The main problem here is that forks do not share memory (and I don't mean copy on write!). This is a problem for servers that need to cache a lot of data, as there is problems sharing data between forks.



Why do I want threads
I am writing a OODBMS in perl. This software needs to be able to scale with processors, and split up work that can be blocking (reading to and from disk) between different threads. I use POE ( an event loop). Now I could use fork, but alot of the data we work on can be cached, and cache sharing between forks is not trivial. Neither is cache invalidations between forks.

$cache = iThread::Shared->shared('HASH'); If I had threads, I could share the cache simply and elegantly between the threads. My code would be portable to all operating systems that support ithreads and I wouldn't have to deal with crufty IPC that differs from platform to platform.


This is why I want threads!

Artur