Over a million developers have joined DZone.

Simple Perl Thread

·
// 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 ) ;
}
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}