I spent some time on the following text earlier this year, figuring it might
be a useful FAQ (maybe).  I posted it to ActivePerl and Perl5-porters back
in March (?) of this year with no substantive response.  I eventually gave
up and decided to not use threads at all until something changed.

I just now found this list, and y'all obviously already know all this stuff,
but I'm still curious about the accuracy of my conclusions and the
availability and utility of threads in Perl.  Since there is at least one
recent query in this list regarding the status of threads in Perl, perhaps
there is yet some utility herein.  It seems like a document similar to this
(but better!) should be packaged with the basic Perl documentation.

Note that there are points where the original text is now outdated.  There
is now a way to work with ithreads from Perl source via the threads-0.03
package.  There is also a way to share data via threads::shared.  This is
all new to me since I last looked into the topic.  I'm excited to find that
some progress is apparently being made towards useful Perl threads.

If anyone shows any interest in this posting, I'm willing to rewrite/update
it to reflect later developments.  Please excuse my ignorance.  And thanks
for all the good work in evidence on this list!

-------------------------------------------------------------------

My original question is:

        What's happening with threads in the Perl 5.x development tree?

Having spent some hours perusing documentation, mailing list archives,
Perl-related sites on the web, and even some Perl source code I'm going to
attempt to answer my own question.  Hopefully someone who really _knows_
will provide an indignant response correcting my errors...

THERE BE TWO

First off, there are currently two different kinds of thread support in
Perl:  the Thread package and ithreads.  These are vastly different thread
support mechanisms.  The former is currently in disfavor and under some sort
of revision.

* Thread.pm

The Thread package provides a Thread object with various fairly normal
methods.  Threads are similar to and also slightly different from whatever
threading model you're used to.  Functions are started in their own threads.
Variables and function calls can be synchronized via semaphores.
Thread-safe queues are provided.  Manipulation of threads from Perl source
code is directly supported.  This is probably (at least in a naive way) what
you really want.

Current releases of Perl (e.g. ActivePerl build 623) document this module
but warn that it is due for (possibly massive) revision.  None of the binary
distributions come with this support enabled.  If you attempt to

        use Thread;

in your code you will get a message like:

        No threads in this perl at ...

This may be considered the "old" thread model.  It came out with Perl v5.005
and all the current (5.6.x) documentation says that it is in disfavor and
may change a lot really soon now.

* iThreads

Interpreter threads, or ithreads, are an entire different mechanism.  With
an ithread, the entire Perl interpreter (or at least the process stack and
global variables that make a specific instantiation unique) are duplicated.

This is similar to the fork()/join() mechanism supported by Unix.  In fact,
using these threads it is possible to provide behavior similar to Unix from
Windows.  This is a _big_ deal (at least to some of us) as now a bunch of
legacy Perl code from Unix will work under Windows.

On the flip side, there is no way to manipulate ithreads directly from Perl
other than fork()/join().  In addition, there are some serious performance
considerations (mostly memory usage) on the Windows platform.

Most importantly, these threads aren't so much threads as pseudo-processes.
On Windows they are _implemented_ as threads, but they don't provide shared
variable space or any of the other usual aspects of using threads.  They
function just the same as Unix processes spawned with fork(), so they may be
threads but they don't provide thread behavior to the programmer.

Current binary distributions come with this support enabled.

This may be considered the "other" thread model.

There is some discussion about evolving the ithread model into something
that can be used in a more conventional manner.  The details are fuzzy, but
this may be the way threading is supposed to go for Perl versions after
5.6.x.

VERSION SUPPORT

* 5.005

This is the version that provided the Thread.pm module.  It was experimental
at that time.  Not all of the modules (even the core modules included in the
Perl distribution) were fixed to be thread-safe.  It was definitely a case
of caveat programmer.

* 5.6.0

This is the version that provided ithreads.  They are intended to provide
fork()/join() to Windows-based Perl.  There are some hints that this may be
incompatible with the 5.005 Thread package.

* 5.6.1

This looks like a maintenance release.  The trial version currently being
distributed doesn't include any more or different thread support.  It is
still going out with 5.005 thread support disabled.

* 5.7.0

I downloaded a copy of the 5.7.0 development tree.  Nothing leaped out at me
from looking at what's in this.  I can't tell if any "new" threads will be
in this release or not.  Of course, I'm not a big Perl internals person and
I might not know what I was looking at.

* 6.0

As I understand it, Perl v6 (also called Topaz?) will be a vastly different
Perl.  Larry Wall has opened the direction of the language to other parties
more than previous versions, and I saw mention of 300+ specific language
comments.

As I understand it, the shape of Perl v6 is currently being hammered out.
If Topaz is the codename for Perl v6 then it is only now beginning to run
programs.  I'm guessing that v6 won't be out before 2002/2003, so it isn't
really on _my_ radar yet.

One thing that may be true about v6:  it looks like the run-time and p-code
are being done to support multiple languages.  In theory it may be possible
to have a Perl v5 parser that runs alongside of a Java parser and have both
generate v6 code.  This might force the v6 multi-threading model (which is
apparently going to be designed in from the ground up) back onto any ongoing
support for v5.  There is also some indication that v5 will continue to live
even after v6 comes out (once again, Perl v6 may not actually be Perl but
something else entirely).

BUILDING FOR THREADS

So, if you want threads that are there, what do you do?  Well, get the
source and build it for yourself!  Normally the ithread (Thread.pm)
emulation should be turned on already, but 5.005 threads will be off.

If you're using Unix, the Configure utility should set the proper build
symbols for you.  I'm working on Windows, so I don't know anything concrete
about Configure.  Sorry.

On Windows (and perhaps some other systems) it is necessary to fix the
Makefile to add the proper symbols to the build.  I've successfully enabled
5.005 threads under Windows in this manner.

Note that the two models are (as of 5.6.0) incompatible.  To the best of my
knowledge.  You can, of course, turn _both_ of them off.

* Thread.pm

The following switch should enable the old thread model:

        USE_5005THREADS

Note that you may need to undefine iThreads symbols in order for this to
work properly.

* iThreads

The following switches should control the iThread model:

        USE_MULTI
        USE_ITHREADS
        USE_IMP_SYS

Of course, these are currently turned on for the binaries I've been
downloading, so you probably don't have to worry about it unless you're on
Unix (where you theoretically don't need them) or you need to turn them off.

CONCLUSION

Currently, there is really _no_ thread support in Perl.  The use of ithreads
to dummy up fork()/join() under Windows is inaccessible directly and in
practice it is really a method of providing multiple _processes_, not
multiple threads.  This is most obvious when you consider the lack of shared
variable space.  If you want threads as such, this isn't what you want.

The older thread model is in disfavor and will change sometime into a "new"
thread model.  So you can build Perl with this turned on, but it won't do
you a lot of good (since the packages aren't thread-safe) and anything you
write will eventually break.

Regarding a third or "new" thread model:  I don't know what it will look
like or when it will be available.  I assume that there is a _lot_ of work
to be done to truly support this, and that may in fact push it off to v6.
Darn it.

The future of threads in Perl is "cloudy" to those of us not in the
development loop.  I would suspect that a group of Perl porters are working
on it, but for what version?  And what will it look like?

Well, like I said earlier, all I can do is show my ignorance...


    ***************************************************
    * Marc M. Adkins             * Software Mercenary *
    * [EMAIL PROTECTED] * Dilettante * Human *
    ***************************************************

Reply via email to