Simple example of Perl threads





9
Date Submitted Thu. Aug. 10th, 2006 2:02 PM
Revision 1 of 1
Beginner sio2man
Tags Perl | Threads
Comments 1 comments
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 );
}

 

Daryl Hoyt

Comments

Comments small typoe
Fri. Feb. 13th, 2009 7:09 AM    Newbie joncollis

Voting