source: trunk/essentials/dev-lang/perl/pod/perlothrtut.pod@ 3397

Last change on this file since 3397 was 3181, checked in by bird, 19 years ago

perl 5.8.8

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