Simple example of Perl threads
9
This is a little example of how to use threads in Perl. It creates three threads and runs them... That's it...
#!/usr/bin/perl
use threads;
use threads::shared;
$|=1;
my ($global):shared;
my (@threads);
push(@threads, threads->new(\&mySub,1));
push(@threads, threads->new(\&mySub,2));
push(@threads, threads->new(\&mySub,3));
$i = 0;
foreach my $myThread(@threads)
{
my @ReturnData = $myTread->join ;
print "Thread $i returned: @ReturnData\n";
$i++;
}
sub mySub
{
my ($threadID) = @_;
for(0..10)
{
$global++;
print "Thread ID: $threadID >> $_ >> GLB: $global\n";
sleep(1);
}
return( $id );
}






There are currently no comments for this snippet.