#!/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>
In reply to Multi-threading in 5.8.0 by DigitalKitty
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |