remove dead code
[p5sagit/p5-mst-13.2.git] / pod / perlthrtut.pod
CommitLineData
2605996a 1=head1 NAME
2
3perlthrtut - tutorial on threads in Perl
4
5=head1 DESCRIPTION
6
7One of the most prominent new features of Perl 5.005 is the inclusion
19799a22 8of threads. Threads make a number of things a lot easier, and are a
2605996a 9very useful addition to your bag of programming tricks.
10
11=head1 What Is A Thread Anyway?
12
13A thread is a flow of control through a program with a single
14execution point.
15
16Sounds an awful lot like a process, doesn't it? Well, it should.
19799a22 17Threads are one of the pieces of a process. Every process has at least
2605996a 18one thread and, up until now, every process running Perl had only one
19799a22 19thread. With 5.005, though, you can create extra threads. We're going
2605996a 20to show you how, when, and why.
21
22=head1 Threaded Program Models
23
24There are three basic ways that you can structure a threaded
19799a22 25program. Which model you choose depends on what you need your program
26to do. For many non-trivial threaded programs you'll need to choose
2605996a 27different models for different pieces of your program.
28
29=head2 Boss/Worker
30
31The boss/worker model usually has one `boss' thread and one or more
19799a22 32`worker' threads. The boss thread gathers or generates tasks that need
2605996a 33to be done, then parcels those tasks out to the appropriate worker
34thread.
35
36This model is common in GUI and server programs, where a main thread
37waits for some event and then passes that event to the appropriate
19799a22 38worker threads for processing. Once the event has been passed on, the
2605996a 39boss thread goes back to waiting for another event.
40
19799a22 41The boss thread does relatively little work. While tasks aren't
2605996a 42necessarily performed faster than with any other method, it tends to
43have the best user-response times.
44
45=head2 Work Crew
46
47In the work crew model, several threads are created that do
19799a22 48essentially the same thing to different pieces of data. It closely
2605996a 49mirrors classical parallel processing and vector processors, where a
50large array of processors do the exact same thing to many pieces of
51data.
52
53This model is particularly useful if the system running the program
19799a22 54will distribute multiple threads across different processors. It can
2605996a 55also be useful in ray tracing or rendering engines, where the
56individual threads can pass on interim results to give the user visual
57feedback.
58
59=head2 Pipeline
60
61The pipeline model divides up a task into a series of steps, and
62passes the results of one step on to the thread processing the
19799a22 63next. Each thread does one thing to each piece of data and passes the
2605996a 64results to the next thread in line.
65
66This model makes the most sense if you have multiple processors so two
67or more threads will be executing in parallel, though it can often
19799a22 68make sense in other contexts as well. It tends to keep the individual
2605996a 69tasks small and simple, as well as allowing some parts of the pipeline
70to block (on I/O or system calls, for example) while other parts keep
19799a22 71going. If you're running different parts of the pipeline on different
2605996a 72processors you may also take advantage of the caches on each
73processor.
74
75This model is also handy for a form of recursive programming where,
76rather than having a subroutine call itself, it instead creates
19799a22 77another thread. Prime and Fibonacci generators both map well to this
2605996a 78form of the pipeline model. (A version of a prime number generator is
79presented later on.)
80
81=head1 Native threads
82
19799a22 83There are several different ways to implement threads on a system. How
2605996a 84threads are implemented depends both on the vendor and, in some cases,
19799a22 85the version of the operating system. Often the first implementation
2605996a 86will be relatively simple, but later versions of the OS will be more
87sophisticated.
88
89While the information in this section is useful, it's not necessary,
90so you can skip it if you don't feel up to it.
91
92There are three basic categories of threads-user-mode threads, kernel
93threads, and multiprocessor kernel threads.
94
95User-mode threads are threads that live entirely within a program and
19799a22 96its libraries. In this model, the OS knows nothing about threads. As
2605996a 97far as it's concerned, your process is just a process.
98
99This is the easiest way to implement threads, and the way most OSes
19799a22 100start. The big disadvantage is that, since the OS knows nothing about
101threads, if one thread blocks they all do. Typical blocking activities
2605996a 102include most system calls, most I/O, and things like sleep().
103
19799a22 104Kernel threads are the next step in thread evolution. The OS knows
2605996a 105about kernel threads, and makes allowances for them. The main
106difference between a kernel thread and a user-mode thread is
19799a22 107blocking. With kernel threads, things that block a single thread don't
108block other threads. This is not the case with user-mode threads,
2605996a 109where the kernel blocks at the process level and not the thread level.
110
111This is a big step forward, and can give a threaded program quite a
112performance boost over non-threaded programs. Threads that block
113performing I/O, for example, won't block threads that are doing other
19799a22 114things. Each process still has only one thread running at once,
2605996a 115though, regardless of how many CPUs a system might have.
116
117Since kernel threading can interrupt a thread at any time, they will
118uncover some of the implicit locking assumptions you may make in your
19799a22 119program. For example, something as simple as C<$a = $a + 2> can behave
120unpredictably with kernel threads if $a is visible to other
121threads, as another thread may have changed $a between the time it
2605996a 122was fetched on the right hand side and the time the new value is
123stored.
124
125Multiprocessor Kernel Threads are the final step in thread
19799a22 126support. With multiprocessor kernel threads on a machine with multiple
2605996a 127CPUs, the OS may schedule two or more threads to run simultaneously on
128different CPUs.
129
130This can give a serious performance boost to your threaded program,
19799a22 131since more than one thread will be executing at the same time. As a
2605996a 132tradeoff, though, any of those nagging synchronization issues that
133might not have shown with basic kernel threads will appear with a
134vengeance.
135
136In addition to the different levels of OS involvement in threads,
137different OSes (and different thread implementations for a particular
138OS) allocate CPU cycles to threads in different ways.
139
140Cooperative multitasking systems have running threads give up control
19799a22 141if one of two things happen. If a thread calls a yield function, it
142gives up control. It also gives up control if the thread does
143something that would cause it to block, such as perform I/O. In a
2605996a 144cooperative multitasking implementation, one thread can starve all the
145others for CPU time if it so chooses.
146
147Preemptive multitasking systems interrupt threads at regular intervals
19799a22 148while the system decides which thread should run next. In a preemptive
2605996a 149multitasking system, one thread usually won't monopolize the CPU.
150
151On some systems, there can be cooperative and preemptive threads
152running simultaneously. (Threads running with realtime priorities
153often behave cooperatively, for example, while threads running at
154normal priorities behave preemptively.)
155
156=head1 What kind of threads are perl threads?
157
158If you have experience with other thread implementations, you might
19799a22 159find that things aren't quite what you expect. It's very important to
2605996a 160remember when dealing with Perl threads that Perl Threads Are Not X
161Threads, for all values of X. They aren't POSIX threads, or
19799a22 162DecThreads, or Java's Green threads, or Win32 threads. There are
2605996a 163similarities, and the broad concepts are the same, but if you start
164looking for implementation details you're going to be either
19799a22 165disappointed or confused. Possibly both.
2605996a 166
167This is not to say that Perl threads are completely different from
19799a22 168everything that's ever come before--they're not. Perl's threading
169model owes a lot to other thread models, especially POSIX. Just as
170Perl is not C, though, Perl threads are not POSIX threads. So if you
2605996a 171find yourself looking for mutexes, or thread priorities, it's time to
172step back a bit and think about what you want to do and how Perl can
173do it.
174
175=head1 Threadsafe Modules
176
177The addition of threads has changed Perl's internals
19799a22 178substantially. There are implications for people who write
179modules--especially modules with XS code or external libraries. While
2605996a 180most modules won't encounter any problems, modules that aren't
181explicitly tagged as thread-safe should be tested before being used in
182production code.
183
184Not all modules that you might use are thread-safe, and you should
185always assume a module is unsafe unless the documentation says
19799a22 186otherwise. This includes modules that are distributed as part of the
187core. Threads are a beta feature, and even some of the standard
2605996a 188modules aren't thread-safe.
189
190If you're using a module that's not thread-safe for some reason, you
191can protect yourself by using semaphores and lots of programming
19799a22 192discipline to control access to the module. Semaphores are covered
2605996a 193later in the article. Perl Threads Are Different
194
195=head1 Thread Basics
196
197The core Thread module provides the basic functions you need to write
19799a22 198threaded programs. In the following sections we'll cover the basics,
199showing you what you need to do to create a threaded program. After
2605996a 200that, we'll go over some of the features of the Thread module that
201make threaded programming easier.
202
203=head2 Basic Thread Support
204
205Thread support is a Perl compile-time option-it's something that's
206turned on or off when Perl is built at your site, rather than when
207your programs are compiled. If your Perl wasn't compiled with thread
208support enabled, then any attempt to use threads will fail.
209
210Remember that the threading support in 5.005 is in beta release, and
19799a22 211should be treated as such. You should expect that it may not function
2605996a 212entirely properly, and the thread interface may well change some
213before it is a fully supported, production release. The beta version
214shouldn't be used for mission-critical projects. Having said that,
215threaded Perl is pretty nifty, and worth a look.
216
217Your programs can use the Config module to check whether threads are
218enabled. If your program can't run without them, you can say something
219like:
220
221 $Config{usethreads} or die "Recompile Perl with threads to run this program.";
222
223A possibly-threaded program using a possibly-threaded module might
224have code like this:
225
226 use Config;
227 use MyMod;
228
229 if ($Config{usethreads}) {
230 # We have threads
231 require MyMod_threaded;
232 import MyMod_threaded;
233 } else {
234 require MyMod_unthreaded;
235 import MyMod_unthreaded;
236 }
237
238Since code that runs both with and without threads is usually pretty
239messy, it's best to isolate the thread-specific code in its own
19799a22 240module. In our example above, that's what MyMod_threaded is, and it's
2605996a 241only imported if we're running on a threaded Perl.
242
243=head2 Creating Threads
244
245The Thread package provides the tools you need to create new
19799a22 246threads. Like any other module, you need to tell Perl you want to use
2605996a 247it; use Thread imports all the pieces you need to create basic
248threads.
249
250The simplest, straightforward way to create a thread is with new():
251
252 use Thread;
253
254 $thr = new Thread \&sub1;
255
256 sub sub1 {
257 print "In the thread\n";
258 }
259
260The new() method takes a reference to a subroutine and creates a new
19799a22 261thread, which starts executing in the referenced subroutine. Control
2605996a 262then passes both to the subroutine and the caller.
263
264If you need to, your program can pass parameters to the subroutine as
19799a22 265part of the thread startup. Just include the list of parameters as
2605996a 266part of the C<Thread::new> call, like this:
267
268 use Thread;
269 $Param3 = "foo";
270 $thr = new Thread \&sub1, "Param 1", "Param 2", $Param3;
271 $thr = new Thread \&sub1, @ParamList;
272 $thr = new Thread \&sub1, qw(Param1 Param2 $Param3);
273
274 sub sub1 {
275 my @InboundParameters = @_;
276 print "In the thread\n";
277 print "got parameters >", join("<>", @InboundParameters), "<\n";
278 }
279
280
281The subroutine runs like a normal Perl subroutine, and the call to new
282Thread returns whatever the subroutine returns.
283
19799a22 284The last example illustrates another feature of threads. You can spawn
285off several threads using the same subroutine. Each thread executes
2605996a 286the same subroutine, but in a separate thread with a separate
287environment and potentially separate arguments.
288
289The other way to spawn a new thread is with async(), which is a way to
290spin off a chunk of code like eval(), but into its own thread:
291
292 use Thread qw(async);
293
294 $LineCount = 0;
295
296 $thr = async {
297 while(<>) {$LineCount++}
298 print "Got $LineCount lines\n";
299 };
300
301 print "Waiting for the linecount to end\n";
302 $thr->join;
303 print "All done\n";
304
305You'll notice we did a use Thread qw(async) in that example. async is
306not exported by default, so if you want it, you'll either need to
307import it before you use it or fully qualify it as
19799a22 308Thread::async. You'll also note that there's a semicolon after the
309closing brace. That's because async() treats the following block as an
2605996a 310anonymous subroutine, so the semicolon is necessary.
311
312Like eval(), the code executes in the same context as it would if it
19799a22 313weren't spun off. Since both the code inside and after the async start
314executing, you need to be careful with any shared resources. Locking
2605996a 315and other synchronization techniques are covered later.
316
317=head2 Giving up control
318
319There are times when you may find it useful to have a thread
19799a22 320explicitly give up the CPU to another thread. Your threading package
2605996a 321might not support preemptive multitasking for threads, for example, or
322you may be doing something compute-intensive and want to make sure
19799a22 323that the user-interface thread gets called frequently. Regardless,
2605996a 324there are times that you might want a thread to give up the processor.
325
326Perl's threading package provides the yield() function that does
327this. yield() is pretty straightforward, and works like this:
328
329 use Thread qw(yield async);
330 async {
331 my $foo = 50;
332 while ($foo--) { print "first async\n" }
333 yield;
334 $foo = 50;
335 while ($foo--) { print "first async\n" }
336 };
337 async {
338 my $foo = 50;
339 while ($foo--) { print "second async\n" }
340 yield;
341 $foo = 50;
342 while ($foo--) { print "second async\n" }
343 };
344
345=head2 Waiting For A Thread To Exit
346
19799a22 347Since threads are also subroutines, they can return values. To wait
2605996a 348for a thread to exit and extract any scalars it might return, you can
349use the join() method.
350
351 use Thread;
352 $thr = new Thread \&sub1;
353
354 @ReturnData = $thr->join;
355 print "Thread returned @ReturnData";
356
357 sub sub1 { return "Fifty-six", "foo", 2; }
358
359In the example above, the join() method returns as soon as the thread
19799a22 360ends. In addition to waiting for a thread to finish and gathering up
2605996a 361any values that the thread might have returned, join() also performs
362any OS cleanup necessary for the thread. That cleanup might be
363important, especially for long-running programs that spawn lots of
19799a22 364threads. If you don't want the return values and don't want to wait
2605996a 365for the thread to finish, you should call the detach() method
366instead. detach() is covered later in the article.
367
368=head2 Errors In Threads
369
370So what happens when an error occurs in a thread? Any errors that
371could be caught with eval() are postponed until the thread is
19799a22 372joined. If your program never joins, the errors appear when your
2605996a 373program exits.
374
375Errors deferred until a join() can be caught with eval():
376
377 use Thread qw(async);
378 $thr = async {$b = 3/0}; # Divide by zero error
379 $foo = eval {$thr->join};
380 if ($@) {
381 print "died with error $@\n";
382 } else {
383 print "Hey, why aren't you dead?\n";
384 }
385
386eval() passes any results from the joined thread back unmodified, so
387if you want the return value of the thread, this is your only chance
388to get them.
389
390=head2 Ignoring A Thread
391
392join() does three things:it waits for a thread to exit, cleans up
19799a22 393after it, and returns any data the thread may have produced. But what
2605996a 394if you're not interested in the thread's return values, and you don't
395really care when the thread finishes? All you want is for the thread
396to get cleaned up after when it's done.
397
19799a22 398In this case, you use the detach() method. Once a thread is detached,
2605996a 399it'll run until it's finished, then Perl will clean up after it
400automatically.
401
402 use Thread;
403 $thr = new Thread \&sub1; # Spawn the thread
404
405 $thr->detach; # Now we officially don't care any more
406
407 sub sub1 {
408 $a = 0;
409 while (1) {
410 $a++;
411 print "\$a is $a\n";
412 sleep 1;
413 }
414 }
415
416
417Once a thread is detached, it may not be joined, and any output that
418it might have produced (if it was done and waiting for a join) is
419lost.
420
421=head1 Threads And Data
422
423Now that we've covered the basics of threads, it's time for our next
19799a22 424topic: data. Threading introduces a couple of complications to data
2605996a 425access that non-threaded programs never need to worry about.
426
427=head2 Shared And Unshared Data
428
429The single most important thing to remember when using threads is that
430all threads potentially have access to all the data anywhere in your
19799a22 431program. While this is true with a nonthreaded Perl program as well,
2605996a 432it's especially important to remember with a threaded program, since
433more than one thread can be accessing this data at once.
434
435Perl's scoping rules don't change because you're using threads. If a
436subroutine (or block, in the case of async()) could see a variable if
19799a22 437you weren't running with threads, it can see it if you are. This is
2605996a 438especially important for the subroutines that create, and makes my
19799a22 439variables even more important. Remember--if your variables aren't
2605996a 440lexically scoped (declared with C<my>) you're probably sharing it between
441threads.
442
443=head2 Thread Pitfall: Races
444
445While threads bring a new set of useful tools, they also bring a
19799a22 446number of pitfalls. One pitfall is the race condition:
2605996a 447
448 use Thread;
449 $a = 1;
450 $thr1 = Thread->new(\&sub1);
451 $thr2 = Thread->new(\&sub2);
452
453 sleep 10;
454 print "$a\n";
455
456 sub sub1 { $foo = $a; $a = $foo + 1; }
457 sub sub2 { $bar = $a; $a = $bar + 1; }
458
459What do you think $a will be? The answer, unfortunately, is "it
460depends." Both sub1() and sub2() access the global variable $a, once
19799a22 461to read and once to write. Depending on factors ranging from your
2605996a 462thread implementation's scheduling algorithm to the phase of the moon,
463$a can be 2 or 3.
464
465Race conditions are caused by unsynchronized access to shared
19799a22 466data. Without explicit synchronization, there's no way to be sure that
2605996a 467nothing has happened to the shared data between the time you access it
19799a22 468and the time you update it. Even this simple code fragment has the
2605996a 469possibility of error:
470
471 use Thread qw(async);
472 $a = 2;
473 async{ $b = $a; $a = $b + 1; };
474 async{ $c = $a; $a = $c + 1; };
475
19799a22 476Two threads both access $a. Each thread can potentially be interrupted
477at any point, or be executed in any order. At the end, $a could be 3
2605996a 478or 4, and both $b and $c could be 2 or 3.
479
480Whenever your program accesses data or resources that can be accessed
481by other threads, you must take steps to coordinate access or risk
482data corruption and race conditions.
483
484=head2 Controlling access: lock()
485
486The lock() function takes a variable (or subroutine, but we'll get to
19799a22 487that later) and puts a lock on it. No other thread may lock the
2605996a 488variable until the locking thread exits the innermost block containing
19799a22 489the lock. Using lock() is straightforward:
2605996a 490
491 use Thread qw(async);
492 $a = 4;
493 $thr1 = async {
494 $foo = 12;
495 {
496 lock ($a); # Block until we get access to $a
497 $b = $a;
498 $a = $b * $foo;
499 }
500 print "\$foo was $foo\n";
501 };
502 $thr2 = async {
503 $bar = 7;
504 {
505 lock ($a); # Block until we can get access to $a
506 $c = $a;
507 $a = $c * $bar;
508 }
509 print "\$bar was $bar\n";
510 };
511 $thr1->join;
512 $thr2->join;
513 print "\$a is $a\n";
514
515lock() blocks the thread until the variable being locked is
19799a22 516available. When lock() returns, your thread can be sure that no other
2605996a 517thread can lock that variable until the innermost block containing the
518lock exits.
519
520It's important to note that locks don't prevent access to the variable
19799a22 521in question, only lock attempts. This is in keeping with Perl's
2605996a 522longstanding tradition of courteous programming, and the advisory file
19799a22 523locking that flock() gives you. Locked subroutines behave differently,
524however. We'll cover that later in the article.
2605996a 525
19799a22 526You may lock arrays and hashes as well as scalars. Locking an array,
2605996a 527though, will not block subsequent locks on array elements, just lock
528attempts on the array itself.
529
530Finally, locks are recursive, which means it's okay for a thread to
19799a22 531lock a variable more than once. The lock will last until the outermost
2605996a 532lock() on the variable goes out of scope.
533
534=head2 Thread Pitfall: Deadlocks
535
19799a22 536Locks are a handy tool to synchronize access to data. Using them
537properly is the key to safe shared data. Unfortunately, locks aren't
538without their dangers. Consider the following code:
2605996a 539
540 use Thread qw(async yield);
541 $a = 4;
542 $b = "foo";
543 async {
544 lock($a);
545 yield;
546 sleep 20;
547 lock ($b);
548 };
549 async {
550 lock($b);
551 yield;
552 sleep 20;
553 lock ($a);
554 };
555
19799a22 556This program will probably hang until you kill it. The only way it
2605996a 557won't hang is if one of the two async() routines acquires both locks
19799a22 558first. A guaranteed-to-hang version is more complicated, but the
2605996a 559principle is the same.
560
561The first thread spawned by async() will grab a lock on $a then, a
19799a22 562second or two later, try to grab a lock on $b. Meanwhile, the second
563thread grabs a lock on $b, then later tries to grab a lock on $a. The
2605996a 564second lock attempt for both threads will block, each waiting for the
565other to release its lock.
566
567This condition is called a deadlock, and it occurs whenever two or
568more threads are trying to get locks on resources that the others
19799a22 569own. Each thread will block, waiting for the other to release a lock
570on a resource. That never happens, though, since the thread with the
2605996a 571resource is itself waiting for a lock to be released.
572
19799a22 573There are a number of ways to handle this sort of problem. The best
2605996a 574way is to always have all threads acquire locks in the exact same
19799a22 575order. If, for example, you lock variables $a, $b, and $c, always lock
576$a before $b, and $b before $c. It's also best to hold on to locks for
2605996a 577as short a period of time to minimize the risks of deadlock.
578
579=head2 Queues: Passing Data Around
580
581A queue is a special thread-safe object that lets you put data in one
582end and take it out the other without having to worry about
19799a22 583synchronization issues. They're pretty straightforward, and look like
2605996a 584this:
585
586 use Thread qw(async);
587 use Thread::Queue;
588
589 my $DataQueue = new Thread::Queue;
590 $thr = async {
591 while ($DataElement = $DataQueue->dequeue) {
592 print "Popped $DataElement off the queue\n";
593 }
594 };
595
596 $DataQueue->enqueue(12);
597 $DataQueue->enqueue("A", "B", "C");
598 $DataQueue->enqueue(\$thr);
599 sleep 10;
600 $DataQueue->enqueue(undef);
601
19799a22 602You create the queue with new Thread::Queue. Then you can add lists of
2605996a 603scalars onto the end with enqueue(), and pop scalars off the front of
19799a22 604it with dequeue(). A queue has no fixed size, and can grow as needed
2605996a 605to hold everything pushed on to it.
606
607If a queue is empty, dequeue() blocks until another thread enqueues
19799a22 608something. This makes queues ideal for event loops and other
2605996a 609communications between threads.
610
611=head1 Threads And Code
612
613In addition to providing thread-safe access to data via locks and
614queues, threaded Perl also provides general-purpose semaphores for
615coarser synchronization than locks provide and thread-safe access to
616entire subroutines.
617
618=head2 Semaphores: Synchronizing Data Access
619
19799a22 620Semaphores are a kind of generic locking mechanism. Unlike lock, which
2605996a 621gets a lock on a particular scalar, Perl doesn't associate any
622particular thing with a semaphore so you can use them to control
19799a22 623access to anything you like. In addition, semaphores can allow more
2605996a 624than one thread to access a resource at once, though by default
625semaphores only allow one thread access at a time.
626
627=over 4
628
629=item Basic semaphores
630
631Semaphores have two methods, down and up. down decrements the resource
632count, while up increments it. down calls will block if the
19799a22 633semaphore's current count would decrement below zero. This program
2605996a 634gives a quick demonstration:
635
636 use Thread qw(yield);
637 use Thread::Semaphore;
638 my $semaphore = new Thread::Semaphore;
639 $GlobalVariable = 0;
640
641 $thr1 = new Thread \&sample_sub, 1;
642 $thr2 = new Thread \&sample_sub, 2;
643 $thr3 = new Thread \&sample_sub, 3;
644
645 sub sample_sub {
646 my $SubNumber = shift @_;
647 my $TryCount = 10;
648 my $LocalCopy;
649 sleep 1;
650 while ($TryCount--) {
651 $semaphore->down;
652 $LocalCopy = $GlobalVariable;
653 print "$TryCount tries left for sub $SubNumber (\$GlobalVariable is $GlobalVariable)\n";
654 yield;
655 sleep 2;
656 $LocalCopy++;
657 $GlobalVariable = $LocalCopy;
658 $semaphore->up;
659 }
660 }
661
19799a22 662The three invocations of the subroutine all operate in sync. The
2605996a 663semaphore, though, makes sure that only one thread is accessing the
664global variable at once.
665
666=item Advanced Semaphores
667
668By default, semaphores behave like locks, letting only one thread
19799a22 669down() them at a time. However, there are other uses for semaphores.
2605996a 670
671Each semaphore has a counter attached to it. down() decrements the
19799a22 672counter and up() increments the counter. By default, semaphores are
2605996a 673created with the counter set to one, down() decrements by one, and
19799a22 674up() increments by one. If down() attempts to decrement the counter
675below zero, it blocks until the counter is large enough. Note that
2605996a 676while a semaphore can be created with a starting count of zero, any
677up() or down() always changes the counter by at least
678one. $semaphore->down(0) is the same as $semaphore->down(1).
679
680The question, of course, is why would you do something like this? Why
681create a semaphore with a starting count that's not one, or why
682decrement/increment it by more than one? The answer is resource
19799a22 683availability. Many resources that you want to manage access for can be
2605996a 684safely used by more than one thread at once.
685
19799a22 686For example, let's take a GUI driven program. It has a semaphore that
2605996a 687it uses to synchronize access to the display, so only one thread is
19799a22 688ever drawing at once. Handy, but of course you don't want any thread
689to start drawing until things are properly set up. In this case, you
2605996a 690can create a semaphore with a counter set to zero, and up it when
691things are ready for drawing.
692
693Semaphores with counters greater than one are also useful for
19799a22 694establishing quotas. Say, for example, that you have a number of
695threads that can do I/O at once. You don't want all the threads
2605996a 696reading or writing at once though, since that can potentially swamp
19799a22 697your I/O channels, or deplete your process' quota of filehandles. You
2605996a 698can use a semaphore initialized to the number of concurrent I/O
699requests (or open files) that you want at any one time, and have your
700threads quietly block and unblock themselves.
701
702Larger increments or decrements are handy in those cases where a
703thread needs to check out or return a number of resources at once.
704
705=back
706
707=head2 Attributes: Restricting Access To Subroutines
708
709In addition to synchronizing access to data or resources, you might
19799a22 710find it useful to synchronize access to subroutines. You may be
2605996a 711accessing a singular machine resource (perhaps a vector processor), or
712find it easier to serialize calls to a particular subroutine than to
713have a set of locks and sempahores.
714
19799a22 715One of the additions to Perl 5.005 is subroutine attributes. The
2605996a 716Thread package uses these to provide several flavors of
19799a22 717serialization. It's important to remember that these attributes are
2605996a 718used in the compilation phase of your program so you can't change a
719subroutine's behavior while your program is actually running.
720
721=head2 Subroutine Locks
722
723The basic subroutine lock looks like this:
724
da2094fd 725 sub test_sub :locked {
2605996a 726 }
727
728This ensures that only one thread will be executing this subroutine at
19799a22 729any one time. Once a thread calls this subroutine, any other thread
2605996a 730that calls it will block until the thread in the subroutine exits
19799a22 731it. A more elaborate example looks like this:
2605996a 732
733 use Thread qw(yield);
734
735 new Thread \&thread_sub, 1;
736 new Thread \&thread_sub, 2;
737 new Thread \&thread_sub, 3;
738 new Thread \&thread_sub, 4;
739
da2094fd 740 sub sync_sub :locked {
2605996a 741 my $CallingThread = shift @_;
742 print "In sync_sub for thread $CallingThread\n";
743 yield;
744 sleep 3;
745 print "Leaving sync_sub for thread $CallingThread\n";
746 }
747
748 sub thread_sub {
749 my $ThreadID = shift @_;
750 print "Thread $ThreadID calling sync_sub\n";
751 sync_sub($ThreadID);
752 print "$ThreadID is done with sync_sub\n";
753 }
754
da2094fd 755The C<locked> attribute tells perl to lock sync_sub(), and if you run
756this, you can see that only one thread is in it at any one time.
2605996a 757
758=head2 Methods
759
760Locking an entire subroutine can sometimes be overkill, especially
19799a22 761when dealing with Perl objects. When calling a method for an object,
2605996a 762for example, you want to serialize calls to a method, so that only one
763thread will be in the subroutine for a particular object, but threads
19799a22 764calling that subroutine for a different object aren't blocked. The
2605996a 765method attribute indicates whether the subroutine is really a method.
766
767 use Thread;
768
769 sub tester {
770 my $thrnum = shift @_;
771 my $bar = new Foo;
772 foreach (1..10) {
773 print "$thrnum calling per_object\n";
774 $bar->per_object($thrnum);
775 print "$thrnum out of per_object\n";
776 yield;
777 print "$thrnum calling one_at_a_time\n";
778 $bar->one_at_a_time($thrnum);
779 print "$thrnum out of one_at_a_time\n";
780 yield;
781 }
782 }
783
784 foreach my $thrnum (1..10) {
785 new Thread \&tester, $thrnum;
786 }
787
788 package Foo;
789 sub new {
790 my $class = shift @_;
791 return bless [@_], $class;
792 }
793
da2094fd 794 sub per_object :locked :method {
2605996a 795 my ($class, $thrnum) = @_;
796 print "In per_object for thread $thrnum\n";
797 yield;
798 sleep 2;
799 print "Exiting per_object for thread $thrnum\n";
800 }
801
da2094fd 802 sub one_at_a_time :locked {
2605996a 803 my ($class, $thrnum) = @_;
804 print "In one_at_a_time for thread $thrnum\n";
805 yield;
806 sleep 2;
807 print "Exiting one_at_a_time for thread $thrnum\n";
808 }
809
810As you can see from the output (omitted for brevity; it's 800 lines)
811all the threads can be in per_object() simultaneously, but only one
812thread is ever in one_at_a_time() at once.
813
814=head2 Locking A Subroutine
815
da2094fd 816You can lock a subroutine as you would lock a variable. Subroutine locks
817work the same as specifying a C<locked> attribute for the subroutine,
2605996a 818and block all access to the subroutine for other threads until the
19799a22 819lock goes out of scope. When the subroutine isn't locked, any number
2605996a 820of threads can be in it at once, and getting a lock on a subroutine
19799a22 821doesn't affect threads already in the subroutine. Getting a lock on a
2605996a 822subroutine looks like this:
823
824 lock(\&sub_to_lock);
825
da2094fd 826Simple enough. Unlike the C<locked> attribute, which is a compile time
827option, locking and unlocking a subroutine can be done at runtime at your
19799a22 828discretion. There is some runtime penalty to using lock(\&sub) instead
da2094fd 829of the C<locked> attribute, so make sure you're choosing the proper
2605996a 830method to do the locking.
831
832You'd choose lock(\&sub) when writing modules and code to run on both
833threaded and unthreaded Perl, especially for code that will run on
19799a22 8345.004 or earlier Perls. In that case, it's useful to have subroutines
2605996a 835that should be serialized lock themselves if they're running threaded,
836like so:
837
838 package Foo;
839 use Config;
840 $Running_Threaded = 0;
841
5311ebfa 842 BEGIN { $Running_Threaded = $Config{'usethreads'} }
2605996a 843
844 sub sub1 { lock(\&sub1) if $Running_Threaded }
845
846
847This way you can ensure single-threadedness regardless of which
848version of Perl you're running.
849
850=head1 General Thread Utility Routines
851
852We've covered the workhorse parts of Perl's threading package, and
853with these tools you should be well on your way to writing threaded
19799a22 854code and packages. There are a few useful little pieces that didn't
2605996a 855really fit in anyplace else.
856
857=head2 What Thread Am I In?
858
859The Thread->self method provides your program with a way to get an
19799a22 860object representing the thread it's currently in. You can use this
2605996a 861object in the same way as the ones returned from the thread creation.
862
863=head2 Thread IDs
864
865tid() is a thread object method that returns the thread ID of the
19799a22 866thread the object represents. Thread IDs are integers, with the main
867thread in a program being 0. Currently Perl assigns a unique tid to
2605996a 868every thread ever created in your program, assigning the first thread
869to be created a tid of 1, and increasing the tid by 1 for each new
870thread that's created.
871
872=head2 Are These Threads The Same?
873
874The equal() method takes two thread objects and returns true
875if the objects represent the same thread, and false if they don't.
876
877=head2 What Threads Are Running?
878
879Thread->list returns a list of thread objects, one for each thread
19799a22 880that's currently running. Handy for a number of things, including
2605996a 881cleaning up at the end of your program:
882
883 # Loop through all the threads
884 foreach $thr (Thread->list) {
885 # Don't join the main thread or ourselves
886 if ($thr->tid && !Thread::equal($thr, Thread->self)) {
887 $thr->join;
888 }
889 }
890
19799a22 891The example above is just for illustration. It isn't strictly
2605996a 892necessary to join all the threads you create, since Perl detaches all
893the threads before it exits.
894
895=head1 A Complete Example
896
897Confused yet? It's time for an example program to show some of the
19799a22 898things we've covered. This program finds prime numbers using threads.
2605996a 899
900 1 #!/usr/bin/perl -w
901 2 # prime-pthread, courtesy of Tom Christiansen
902 3
903 4 use strict;
904 5
905 6 use Thread;
906 7 use Thread::Queue;
907 8
908 9 my $stream = new Thread::Queue;
909 10 my $kid = new Thread(\&check_num, $stream, 2);
910 11
911 12 for my $i ( 3 .. 1000 ) {
912 13 $stream->enqueue($i);
913 14 }
914 15
915 16 $stream->enqueue(undef);
916 17 $kid->join();
917 18
918 19 sub check_num {
919 20 my ($upstream, $cur_prime) = @_;
920 21 my $kid;
921 22 my $downstream = new Thread::Queue;
922 23 while (my $num = $upstream->dequeue) {
923 24 next unless $num % $cur_prime;
924 25 if ($kid) {
925 26 $downstream->enqueue($num);
926 27 } else {
927 28 print "Found prime $num\n";
928 29 $kid = new Thread(\&check_num, $downstream, $num);
929 30 }
930 31 }
931 32 $downstream->enqueue(undef) if $kid;
932 33 $kid->join() if $kid;
933 34 }
934
19799a22 935This program uses the pipeline model to generate prime numbers. Each
2605996a 936thread in the pipeline has an input queue that feeds numbers to be
937checked, a prime number that it's responsible for, and an output queue
19799a22 938that it funnels numbers that have failed the check into. If the thread
2605996a 939has a number that's failed its check and there's no child thread, then
19799a22 940the thread must have found a new prime number. In that case, a new
2605996a 941child thread is created for that prime and stuck on the end of the
942pipeline.
943
944This probably sounds a bit more confusing than it really is, so lets
945go through this program piece by piece and see what it does. (For
946those of you who might be trying to remember exactly what a prime
947number is, it's a number that's only evenly divisible by itself and 1)
948
949The bulk of the work is done by the check_num() subroutine, which
950takes a reference to its input queue and a prime number that it's
19799a22 951responsible for. After pulling in the input queue and the prime that
2605996a 952the subroutine's checking (line 20), we create a new queue (line 22)
953and reserve a scalar for the thread that we're likely to create later
954(line 21).
955
956The while loop from lines 23 to line 31 grabs a scalar off the input
957queue and checks against the prime this thread is responsible
19799a22 958for. Line 24 checks to see if there's a remainder when we modulo the
959number to be checked against our prime. If there is one, the number
2605996a 960must not be evenly divisible by our prime, so we need to either pass
961it on to the next thread if we've created one (line 26) or create a
962new thread if we haven't.
963
19799a22 964The new thread creation is line 29. We pass on to it a reference to
2605996a 965the queue we've created, and the prime number we've found.
966
967Finally, once the loop terminates (because we got a 0 or undef in the
968queue, which serves as a note to die), we pass on the notice to our
969child and wait for it to exit if we've created a child (Lines 32 and
97037).
971
972Meanwhile, back in the main thread, we create a queue (line 9) and the
973initial child thread (line 10), and pre-seed it with the first prime:
19799a22 9742. Then we queue all the numbers from 3 to 1000 for checking (lines
2605996a 97512-14), then queue a die notice (line 16) and wait for the first child
19799a22 976thread to terminate (line 17). Because a child won't die until its
2605996a 977child has died, we know that we're done once we return from the join.
978
19799a22 979That's how it works. It's pretty simple; as with many Perl programs,
2605996a 980the explanation is much longer than the program.
981
982=head1 Conclusion
983
984A complete thread tutorial could fill a book (and has, many times),
19799a22 985but this should get you well on your way. The final authority on how
2605996a 986Perl's threads behave is the documention bundled with the Perl
987distribution, but with what we've covered in this article, you should
988be well on your way to becoming a threaded Perl expert.
989
990=head1 Bibliography
991
992Here's a short bibliography courtesy of Jürgen Christoffel:
993
994=head2 Introductory Texts
995
996Birrell, Andrew D. An Introduction to Programming with
997Threads. Digital Equipment Corporation, 1989, DEC-SRC Research Report
998#35 online as
999http://www.research.digital.com/SRC/staff/birrell/bib.html (highly
1000recommended)
1001
1002Robbins, Kay. A., and Steven Robbins. Practical Unix Programming: A
1003Guide to Concurrency, Communication, and
1004Multithreading. Prentice-Hall, 1996.
1005
1006Lewis, Bill, and Daniel J. Berg. Multithreaded Programming with
1007Pthreads. Prentice Hall, 1997, ISBN 0-13-443698-9 (a well-written
1008introduction to threads).
1009
1010Nelson, Greg (editor). Systems Programming with Modula-3. Prentice
1011Hall, 1991, ISBN 0-13-590464-1.
1012
1013Nichols, Bradford, Dick Buttlar, and Jacqueline Proulx Farrell.
1014Pthreads Programming. O'Reilly & Associates, 1996, ISBN 156592-115-1
1015(covers POSIX threads).
1016
1017=head2 OS-Related References
1018
1019Boykin, Joseph, David Kirschen, Alan Langerman, and Susan
1020LoVerso. Programming under Mach. Addison-Wesley, 1994, ISBN
10210-201-52739-1.
1022
1023Tanenbaum, Andrew S. Distributed Operating Systems. Prentice Hall,
10241995, ISBN 0-13-143934-0 (great textbook).
1025
1026Silberschatz, Abraham, and Peter B. Galvin. Operating System Concepts,
10274th ed. Addison-Wesley, 1995, ISBN 0-201-59292-4
1028
1029=head2 Other References
1030
1031Arnold, Ken and James Gosling. The Java Programming Language, 2nd
1032ed. Addison-Wesley, 1998, ISBN 0-201-31006-6.
1033
1034Le Sergent, T. and B. Berthomieu. "Incremental MultiThreaded Garbage
1035Collection on Virtually Shared Memory Architectures" in Memory
1036Management: Proc. of the International Workshop IWMM 92, St. Malo,
1037France, September 1992, Yves Bekkers and Jacques Cohen, eds. Springer,
10381992, ISBN 3540-55940-X (real-life thread applications).
1039
1040=head1 Acknowledgements
1041
1042Thanks (in no particular order) to Chaim Frenkel, Steve Fink, Gurusamy
1043Sarathy, Ilya Zakharevich, Benjamin Sugars, Jürgen Christoffel, Joshua
1044Pritikin, and Alan Burlison, for their help in reality-checking and
19799a22 1045polishing this article. Big thanks to Tom Christiansen for his rewrite
2605996a 1046of the prime number generator.
1047
1048=head1 AUTHOR
1049
1050Dan Sugalski E<lt>sugalskd@ous.eduE<gt>
1051
1052=head1 Copyrights
1053
1054This article originally appeared in The Perl Journal #10, and is
1055copyright 1998 The Perl Journal. It appears courtesy of Jon Orwant and
1056The Perl Journal. This document may be distributed under the same terms
1057as Perl itself.
1058
1059