| 1 | =head1 NAME
|
|---|
| 2 |
|
|---|
| 3 | perlothrtut - old tutorial on threads in Perl
|
|---|
| 4 |
|
|---|
| 5 | =head1 DESCRIPTION
|
|---|
| 6 |
|
|---|
| 7 | B<WARNING>:
|
|---|
| 8 | This tutorial describes the old-style thread model that was introduced in
|
|---|
| 9 | release 5.005. This model is now deprecated, and will be removed, probably
|
|---|
| 10 | in version 5.10. The interfaces described here were considered
|
|---|
| 11 | experimental, and are likely to be buggy.
|
|---|
| 12 |
|
|---|
| 13 | For information about the new interpreter threads ("ithreads") model, see
|
|---|
| 14 | the F<perlthrtut> tutorial, and the L<threads> and L<threads::shared>
|
|---|
| 15 | modules.
|
|---|
| 16 |
|
|---|
| 17 | You are strongly encouraged to migrate any existing threads code to the
|
|---|
| 18 | new model as soon as possible.
|
|---|
| 19 |
|
|---|
| 20 | =head1 What Is A Thread Anyway?
|
|---|
| 21 |
|
|---|
| 22 | A thread is a flow of control through a program with a single
|
|---|
| 23 | execution point.
|
|---|
| 24 |
|
|---|
| 25 | Sounds an awful lot like a process, doesn't it? Well, it should.
|
|---|
| 26 | Threads are one of the pieces of a process. Every process has at least
|
|---|
| 27 | one thread and, up until now, every process running Perl had only one
|
|---|
| 28 | thread. With 5.005, though, you can create extra threads. We're going
|
|---|
| 29 | to show you how, when, and why.
|
|---|
| 30 |
|
|---|
| 31 | =head1 Threaded Program Models
|
|---|
| 32 |
|
|---|
| 33 | There are three basic ways that you can structure a threaded
|
|---|
| 34 | program. Which model you choose depends on what you need your program
|
|---|
| 35 | to do. For many non-trivial threaded programs you'll need to choose
|
|---|
| 36 | different models for different pieces of your program.
|
|---|
| 37 |
|
|---|
| 38 | =head2 Boss/Worker
|
|---|
| 39 |
|
|---|
| 40 | The boss/worker model usually has one `boss' thread and one or more
|
|---|
| 41 | `worker' threads. The boss thread gathers or generates tasks that need
|
|---|
| 42 | to be done, then parcels those tasks out to the appropriate worker
|
|---|
| 43 | thread.
|
|---|
| 44 |
|
|---|
| 45 | This model is common in GUI and server programs, where a main thread
|
|---|
| 46 | waits for some event and then passes that event to the appropriate
|
|---|
| 47 | worker threads for processing. Once the event has been passed on, the
|
|---|
| 48 | boss thread goes back to waiting for another event.
|
|---|
| 49 |
|
|---|
| 50 | The boss thread does relatively little work. While tasks aren't
|
|---|
| 51 | necessarily performed faster than with any other method, it tends to
|
|---|
| 52 | have the best user-response times.
|
|---|
| 53 |
|
|---|
| 54 | =head2 Work Crew
|
|---|
| 55 |
|
|---|
| 56 | In the work crew model, several threads are created that do
|
|---|
| 57 | essentially the same thing to different pieces of data. It closely
|
|---|
| 58 | mirrors classical parallel processing and vector processors, where a
|
|---|
| 59 | large array of processors do the exact same thing to many pieces of
|
|---|
| 60 | data.
|
|---|
| 61 |
|
|---|
| 62 | This model is particularly useful if the system running the program
|
|---|
| 63 | will distribute multiple threads across different processors. It can
|
|---|
| 64 | also be useful in ray tracing or rendering engines, where the
|
|---|
| 65 | individual threads can pass on interim results to give the user visual
|
|---|
| 66 | feedback.
|
|---|
| 67 |
|
|---|
| 68 | =head2 Pipeline
|
|---|
|
|---|