DigitalKitty has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; use threads qw( yield ); my $thread1 = threads->new(\&sub1); my $thread2 = threads->new(\&sub2); my @data = $thread1->join; print "'Thread1' returned @data"; my @data2 = $thread2->join; print "'Thread2' returned @data2"; sub sub1 { yield; sleep 1; print "I'm a perl programmer!\n"; return "Threading in perl is fun!\n"; } sub sub2 { print "I like computing!\n"; return "Computing is cool!\n"; }
Sample output without using sleep ( win98 - ActiveState 5.8.0 build ): C:\Perl\bin\hw>perl pg5.pl I'm a perl programmer! I like computing! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>perl pg5.pl I'm a perl programmer! I like computing! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>perl pg5.pl I'm a perl programmer! I like computing! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>perl pg5.pl I'm a perl programmer! I like computing! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>
From what I understand, perl 6 is going to include an improved thread model. Very interesting indeed.Sample output with sleep and yield: C:\Perl\bin\hw>perl pg5.pl I like computing! *pause for 1 second* I'm a perl programmer! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>perl pg5.pl I like computing! I'm a perl programmer! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>perl pg5.pl I like computing! I'm a perl programmer! 'Thread1' returned Threading in perl is fun! 'Thread2' returned Computing is cool! C:\Perl\bin\hw>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multi-threading in 5.8.0
by pfaut (Priest) on Apr 20, 2003 at 00:01 UTC | |
|
Re: Multi-threading in 5.8.0
by JamesNC (Chaplain) on Apr 20, 2003 at 03:10 UTC | |
|
Re: Multi-threading in 5.8.0
by halley (Prior) on Apr 20, 2003 at 13:14 UTC | |
|
Re: Multi-threading in 5.8.0
by petesmiley (Friar) on Apr 21, 2003 at 15:58 UTC |