This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Obituary for Greg McCarroll.
[perl5.git] / pod / perlthrtut.pod
index fb947b6..e885bb2 100644 (file)
@@ -7,13 +7,13 @@ perlthrtut - Tutorial on threads in Perl
 =head1 DESCRIPTION
 
 This tutorial describes the use of Perl interpreter threads (sometimes
-referred to as I<ithreads>) that was first introduced in Perl 5.6.0.  In this
+referred to as I<ithreads>).  In this
 model, each thread runs in its own Perl interpreter, and any data sharing
 between threads must be explicit.  The user-level interface for I<ithreads>
 uses the L<threads> class.
 
 B<NOTE>: There was another older Perl threading flavor called the 5.005 model
-that used the L<Threads> class.  This old model was known to have problems, is
+that used the L<threads> class.  This old model was known to have problems, is
 deprecated, and was removed for release 5.10.  You are
 strongly encouraged to migrate any existing 5.005 threads code to the new
 model as soon as possible.
@@ -173,7 +173,8 @@ enabled. If your program can't run without them, you can say something
 like:
 
     use Config;
-    $Config{useithreads} or die('Recompile Perl with threads to run this program.');
+    $Config{useithreads} or
+        die('Recompile Perl with threads to run this program.');
 
 A possibly-threaded program using a possibly-threaded module might
 have code like this:
@@ -241,7 +242,7 @@ part of the C<threads-E<gt>create()> call, like this:
     sub sub1 {
         my @InboundParameters = @_;
         print("In the thread\n");
-        print('Got parameters >', join('<>', @InboundParameters), "<\n");
+        print('Got parameters >', join('<>',@InboundParameters), "<\n");
     }
 
 The last example illustrates another feature of threads.  You can spawn
@@ -402,7 +403,8 @@ assignment will cause the thread to die. For example:
 
     ... create some threads ...
 
-    $hash{a} = 1;       # All threads see exists($hash{a}) and $hash{a} == 1
+    $hash{a} = 1;       # All threads see exists($hash{a})
+                        # and $hash{a} == 1
     $hash{a} = $var;    # okay - copy-by-value: same effect as previous
     $hash{a} = $svar;   # okay - copy-by-value: same effect as previous
     $hash{a} = \$svar;  # okay - a reference to a shared variable
@@ -674,7 +676,8 @@ gives a quick demonstration:
         while ($TryCount--) {
             $semaphore->down();
             $LocalCopy = $GlobalVariable;
-            print("$TryCount tries left for sub $SubNumber (\$GlobalVariable is $GlobalVariable)\n");
+            print("$TryCount tries left for sub $SubNumber "
+                 ."(\$GlobalVariable is $GlobalVariable)\n");
             sleep(2);
             $LocalCopy++;
             $GlobalVariable = $LocalCopy;
@@ -846,40 +849,40 @@ does not appear in the list returned by C<threads-E<gt>list()>.
 Confused yet? It's time for an example program to show some of the
 things we've covered.  This program finds prime numbers using threads.
 
-     1 #!/usr/bin/perl
-     2 # prime-pthread, courtesy of Tom Christiansen
-     3
-     4 use strict;
-     5 use warnings;
-     6
-     7 use threads;
-     8 use Thread::Queue;
-     9
-    10 sub check_num {
-    11     my ($upstream, $cur_prime) = @_;
-    12     my $kid;
-    13     my $downstream = Thread::Queue->new();
-    14     while (my $num = $upstream->dequeue()) {
-    15         next unless ($num % $cur_prime);
-    16         if ($kid) {
-    17             $downstream->enqueue($num);
-    18         } else {
-    19             print("Found prime: $num\n");
-    20             $kid = threads->create(\&check_num, $downstream, $num);
-    21             if (! $kid) {
-    22                 warn("Sorry.  Ran out of threads.\n");
-    23                 last;
-    24             }
-    25         }
-    26     }
-    27     if ($kid) {
-    28         $downstream->enqueue(undef);
-    29         $kid->join();
-    30     }
-    31 }
-    32
-    33 my $stream = Thread::Queue->new(3..1000, undef);
-    34 check_num($stream, 2);
+   1 #!/usr/bin/perl
+   2 # prime-pthread, courtesy of Tom Christiansen
+   3
+   4 use strict;
+   5 use warnings;
+   6
+   7 use threads;
+   8 use Thread::Queue;
+   9
+  10 sub check_num {
+  11     my ($upstream, $cur_prime) = @_;
+  12     my $kid;
+  13     my $downstream = Thread::Queue->new();
+  14     while (my $num = $upstream->dequeue()) {
+  15         next unless ($num % $cur_prime);
+  16         if ($kid) {
+  17             $downstream->enqueue($num);
+  18         } else {
+  19             print("Found prime: $num\n");
+  20             $kid = threads->create(\&check_num, $downstream, $num);
+  21             if (! $kid) {
+  22                 warn("Sorry.  Ran out of threads.\n");
+  23                 last;
+  24             }
+  25         }
+  26     }
+  27     if ($kid) {
+  28         $downstream->enqueue(undef);
+  29         $kid->join();
+  30     }
+  31 }
+  32
+  33 my $stream = Thread::Queue->new(3..1000, undef);
+  34 check_num($stream, 2);
 
 This program uses the pipeline model to generate prime numbers.  Each
 thread in the pipeline has an input queue that feeds numbers to be
@@ -1043,7 +1046,7 @@ give you the full POSIX API).  For example, there is no way to
 guarantee that a signal sent to a multi-threaded Perl application
 will get intercepted by any particular thread.  (However, a recently
 added feature does provide the capability to send signals between
-threads.  See L<threads/"THREAD SIGNALLING> for more details.)
+threads.  See L<threads/THREAD SIGNALLING> for more details.)
 
 =head1 Thread-Safety of System Libraries
 
@@ -1080,17 +1083,17 @@ on your way to becoming a threaded Perl expert.
 Annotated POD for L<threads>:
 L<http://annocpan.org/?mode=search&field=Module&name=threads>
 
-Lastest version of L<threads> on CPAN:
+Latest version of L<threads> on CPAN:
 L<http://search.cpan.org/search?module=threads>
 
 Annotated POD for L<threads::shared>:
 L<http://annocpan.org/?mode=search&field=Module&name=threads%3A%3Ashared>
 
-Lastest version of L<threads::shared> on CPAN:
+Latest version of L<threads::shared> on CPAN:
 L<http://search.cpan.org/search?module=threads%3A%3Ashared>
 
 Perl threads mailing list:
-L<http://lists.cpan.org/showlist.cgi?name=iThreads>
+L<http://lists.perl.org/list/ithreads.html>
 
 =head1 Bibliography