X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlothrtut.pod;h=3253097ec05fc46a46a1b208313051b74db17041;hb=71c4dbc37189d1d137ba8e40103273462dd96945;hp=833f0c42cf41aa5afdf959ea606e68ae98a19338;hpb=53d7eaa81952a67657b7bc981fed8fadac42788d;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlothrtut.pod b/pod/perlothrtut.pod index 833f0c4..3253097 100644 --- a/pod/perlothrtut.pod +++ b/pod/perlothrtut.pod @@ -4,17 +4,18 @@ perlothrtut - old tutorial on threads in Perl =head1 DESCRIPTION -B: Threading is an experimental feature. Both the interface -and implementation are subject to change drastically. In fact, this -documentation describes the flavor of threads that was in version -5.005. Perl 5.6.0 and later have the beginnings of support for -interpreter threads, which (when finished) is expected to be -significantly different from what is described here. The information -contained here may therefore soon be obsolete. Use at your own risk! - -One of the most prominent new features of Perl 5.005 is the inclusion -of threads. Threads make a number of things a lot easier, and are a -very useful addition to your bag of programming tricks. +B: +This tutorial describes the old-style thread model that was introduced in +release 5.005. This model is deprecated, and has been removed +for version 5.10. The interfaces described here were considered +experimental, and are likely to be buggy. + +For information about the new interpreter threads ("ithreads") model, see +the F tutorial, and the L and L +modules. + +You are strongly encouraged to migrate any existing threads code to the +new model as soon as possible. =head1 What Is A Thread Anyway? @@ -259,7 +260,7 @@ The simplest, straightforward way to create a thread is with new(): use Thread; - $thr = new Thread \&sub1; + $thr = Thread->new( \&sub1 ); sub sub1 { print "In the thread\n"; @@ -275,9 +276,9 @@ part of the C call, like this: use Thread; $Param3 = "foo"; - $thr = new Thread \&sub1, "Param 1", "Param 2", $Param3; - $thr = new Thread \&sub1, @ParamList; - $thr = new Thread \&sub1, qw(Param1 Param2 $Param3); + $thr = Thread->new( \&sub1, "Param 1", "Param 2", $Param3 ); + $thr = Thread->new( \&sub1, @ParamList ); + $thr = Thread->new( \&sub1, qw(Param1 Param2 $Param3) ); sub sub1 { my @InboundParameters = @_; @@ -357,7 +358,7 @@ for a thread to exit and extract any scalars it might return, you can use the join() method. use Thread; - $thr = new Thread \&sub1; + $thr = Thread->new( \&sub1 ); @ReturnData = $thr->join; print "Thread returned @ReturnData"; @@ -408,7 +409,7 @@ it'll run until it's finished, then Perl will clean up after it automatically. use Thread; - $thr = new Thread \&sub1; # Spawn the thread + $thr = Thread->new( \&sub1 ); # Spawn the thread $thr->detach; # Now we officially don't care any more @@ -594,7 +595,7 @@ this: use Thread qw(async); use Thread::Queue; - my $DataQueue = new Thread::Queue; + my $DataQueue = Thread::Queue->new(); $thr = async { while ($DataElement = $DataQueue->dequeue) { print "Popped $DataElement off the queue\n"; @@ -603,7 +604,6 @@ this: $DataQueue->enqueue(12); $DataQueue->enqueue("A", "B", "C"); - $DataQueue->enqueue(\$thr); sleep 10; $DataQueue->enqueue(undef); @@ -643,12 +643,12 @@ gives a quick demonstration: use Thread qw(yield); use Thread::Semaphore; - my $semaphore = new Thread::Semaphore; + my $semaphore = Thread::Semaphore->new(); $GlobalVariable = 0; - $thr1 = new Thread \&sample_sub, 1; - $thr2 = new Thread \&sample_sub, 2; - $thr3 = new Thread \&sample_sub, 3; + $thr1 = Thread->new( \&sample_sub, 1 ); + $thr2 = Thread->new( \&sample_sub, 2 ); + $thr3 = Thread->new( \&sample_sub, 3 ); sub sample_sub { my $SubNumber = shift @_; @@ -776,7 +776,7 @@ method attribute indicates whether the subroutine is really a method. sub tester { my $thrnum = shift @_; - my $bar = new Foo; + my $bar = Foo->new(); foreach (1..10) { print "$thrnum calling per_object\n"; $bar->per_object($thrnum); @@ -913,8 +913,8 @@ things we've covered. This program finds prime numbers using threads. 6 use Thread; 7 use Thread::Queue; 8 - 9 my $stream = new Thread::Queue; - 10 my $kid = new Thread(\&check_num, $stream, 2); + 9 my $stream = Thread::Queue->new(); + 10 my $kid = Thread->new(\&check_num, $stream, 2); 11 12 for my $i ( 3 .. 1000 ) { 13 $stream->enqueue($i); @@ -926,14 +926,14 @@ things we've covered. This program finds prime numbers using threads. 19 sub check_num { 20 my ($upstream, $cur_prime) = @_; 21 my $kid; - 22 my $downstream = new Thread::Queue; + 22 my $downstream = Thread::Queue->new(); 23 while (my $num = $upstream->dequeue) { 24 next unless $num % $cur_prime; 25 if ($kid) { 26 $downstream->enqueue($num); 27 } else { 28 print "Found prime $num\n"; - 29 $kid = new Thread(\&check_num, $downstream, $num); + 29 $kid = Thread->new(\&check_num, $downstream, $num); 30 } 31 } 32 $downstream->enqueue(undef) if $kid;