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