perlothrtut - old tutorial on threads in Perl
WARNING: This tutorial describes the old-style thread model that was introduced in
release 5.005. This model is now deprecated, and will be removed, probably in version 5.10.
The interfaces described here were considered experimental, and are likely to be buggy.
For information about the new interpreter threads ("ithreads") model, see the perlthrtut
tutorial, and the threads and threads::shared modules.
You are strongly encouraged to migrate any existing threads code to the new model as soon
A thread is a flow of control through a program with a single execution point.
Sounds an awful lot like a process, doesn't it? Well, it should. Threads are one of the
pieces of a process. Every process has at least one thread and, up until now, every process
running Perl had only one thread. With 5.005, though, you can create extra threads. We're
going to show you how, when, and why.
If you have experience with other thread implementations, you might find that things aren't
quite what you expect. It's very important to remember when dealing with Perl threads that
Perl Threads Are Not X Threads, for all values of X. They aren't POSIX threads, or DecThreads,
or Java's Green threads, or Win32 threads. There are similarities, and the broad concepts are
the same, but if you start looking for implementation details you're going to be either
disappointed or confused. Possibly both.
This is not to say that Perl threads are completely different from everything that's ever
come before--they're not. Perl's threading model owes a lot to other thread models, especially
POSIX. Just as Perl is not C, though, Perl threads are not POSIX threads. So if you find
yourself looking for mutexes, or thread priorities, it's time to step back a bit and think
about what you want to do and how Perl can do it.
The addition of threads has changed Perl's internals substantially. There are implications
for people who write modules--especially modules with XS code or external libraries. While
most modules won't encounter any problems, modules that aren't explicitly tagged as
thread-safe should be tested before being used in production code.
Not all modules that you might use are thread-safe, and you should always assume a module
is unsafe unless the documentation says otherwise. This includes modules that are distributed
as part of the core. Threads are a beta feature, and even some of the standard modules aren't
If you're using a module that's not thread-safe for some reason, you can protect yourself
by using semaphores and lots of programming discipline to control access to the module.
Semaphores are covered later in the article. Perl Threads Are Different
A complete thread tutorial could fill a book (and has, many times), but this should get you
well on your way. The final authority on how Perl's threads behave is the documentation
bundled with the Perl distribution, but with what we've covered in this article, you should be
well on your way to becoming a threaded Perl expert.
Here's a short bibliography courtesy of Jürgen Christoffel:
Birrell, Andrew D. An Introduction to Programming with Threads. Digital Equipment
Corporation, 1989, DEC-SRC Research Report #35 online as http://www.research.digital.com/SRC/staff/birrell/bib.html
Robbins, Kay. A., and Steven Robbins. Practical Unix Programming: A Guide to Concurrency,
Communication, and Multithreading. Prentice-Hall, 1996.
Lewis, Bill, and Daniel J. Berg. Multithreaded Programming with Pthreads. Prentice Hall,
1997, ISBN 0-13-443698-9 (a well-written introduction to threads).
Nelson, Greg (editor). Systems Programming with Modula-3. Prentice Hall, 1991, ISBN
Nichols, Bradford, Dick Buttlar, and Jacqueline Proulx Farrell. Pthreads Programming.
O'Reilly & Associates, 1996, ISBN 156592-115-1 (covers POSIX threads).
Boykin, Joseph, David Kirschen, Alan Langerman, and Susan LoVerso. Programming under Mach.
Addison-Wesley, 1994, ISBN 0-201-52739-1.
Tanenbaum, Andrew S. Distributed Operating Systems. Prentice Hall, 1995, ISBN 0-13-219908-4
Silberschatz, Abraham, and Peter B. Galvin. Operating System Concepts, 4th ed.
Addison-Wesley, 1995, ISBN 0-201-59292-4
Arnold, Ken and James Gosling. The Java Programming Language, 2nd ed. Addison-Wesley, 1998,
Le Sergent, T. and B. Berthomieu. "Incremental MultiThreaded Garbage Collection on
Virtually Shared Memory Architectures" in Memory Management: Proc. of the International
Workshop IWMM 92, St. Malo, France, September 1992, Yves Bekkers and Jacques Cohen, eds.
Springer, 1992, ISBN 3540-55940-X (real-life thread applications).
Thanks (in no particular order) to Chaim Frenkel, Steve Fink, Gurusamy Sarathy, Ilya
Zakharevich, Benjamin Sugars, Jürgen Christoffel, Joshua Pritikin, and Alan Burlison, for
their help in reality-checking and polishing this article. Big thanks to Tom Christiansen for
his rewrite of the prime number generator.
Dan Sugalski <firstname.lastname@example.org>
This article originally appeared in The Perl Journal #10, and is copyright 1998 The Perl
Journal. It appears courtesy of Jon Orwant and The Perl Journal. This document may be
distributed under the same terms as Perl itself.