Perl has supported threading for years


A recent column in Software Development Times lamented the lack of support for threading in software today. In it, Andrew Binstock says:

Dynamic languages are even further behind. To wit, Python, allows only one thread to run at a time (except for I/O); this means you can have threads but not running in parallel. Ruby can run threads only within the one VM, which is arguably better but nowhere near good enough. And OpenMP, which might be a solution for some, is limited to C++ or Fortran.

Unfortunately, Binstock ignores the dynamic language that should be first on his mind, Perl. Perl's threading got stable back with Perl 5.8.7 in 2005, after a few years of experimental support. Today, Mac OS X and most Linux distributions ship with a threaded Perl enabled.

How can you tell if your Perl supports threads? Look at the output of perl -V for the string "useithreads":

$ perl -V | grep ithreads
    config_args='-ds -e -Dprefix=/usr -Dccflags=-g  -pipe  -Dldflags=-Dman3ext=3pm -Duseithreads -Duseshrplib'
    usethreads=define use5005threads=undef useithreads=define usemultiplicity=define

While I agree with the basic premise that demand for threaded apps on the desktop is low, the tools for supporting threads are not quite as sparse as Binstock would have us think.

P.S. O'Reilly & Associates is now O'Reilly Media, and has been since April 2004.


The statement "High-level support is notably weak", referring to Java and .NET, is the most significant. I don't want to setup separate threads in my application. I just want to put a 'parallel' flag or something on a foreach loop and let the compiler take care of it.

While perl 5.8's iThreads may be stable, there are a whole bunch of problems with them that the unitiated can trip over.

Problems include:

  • Automatic cloning of non-shared data to new threads

  • The tie() magic that implements shared variables clobbers initial values.

  • The tie() magic that implements shared variables doesn't work well for some types of data.

Head over to PerlMonks and do a Super Search for 'Threads' to find out about them.

Check out Tim Brays Wide Finder project and Frederik Lundhs Python solution for an example of how threading isn't the only way - or neccessarily the fastest way.

Yes, Perl has threads, but have you used them? They're so heavy that they seem all but useless IMHO. Want to double the memory footprint of your Perl app? Spawn a thread. I believe this is because of the "Automatic cloning of non-shared data to new threads" that noted in his post.

That was David Toso's post. Not mine.

Re: Wide Finder, it appears threading may not be the best solution simply because of the implementation of multithreading. Mauricio used a join calculus multithreading (multiprocessing actually) library in OCaml to write an implementation of Wide Finder that's three times as fast.

Leave a comment

Job hunting for programmers

Land the Tech Job You Love, Andy Lester's guide to job hunting for programmers and other technical professionals, is available in PDF, ePub and .mobi formats, all DRM-free, as well as good old-fashioned paper.