DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Simple Perl Thread

02.13.2006
| 1291 views |
  • submit to reddit
        // I don't remember where I picked this up but it is about as simple
// as you will find for threaded perl

#!/usr/bin/perl

use threads;
use threads::shared;

$|=1;
  
my ($global) : shared ;
  
my $thr1 = threads->new(\&TEST,1) ;
my $thr2 = threads->new(\&TEST,2) ;  

my @ReturnData = $thr1->join ;
print "Thread 1 returned: @ReturnData\n" ;

my @ReturnData = $thr2->join ;
print "Thread 2 returned: @ReturnData\n" ;
  
########
# TEST #
########

sub TEST {
  my ( $id ) = @_ ;
  
  for(0..10) {
    $global++ ;
    print "id: $id >> $_ >> GLB: $global\n" ;
    sleep(1) ;
  }

  return( $id ) ;
}