#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use threads; use threads::shared; my %count : shared; my $maxthreads = 3; # there are way more lines than this but anyway... for my $i ( 1 .. 500 ) { redo if ( scalar threads->list() >= $maxthreads ); # this just represents that I've found a pair and want to run the sub if ( $i % 50 == 0 ) { # run the sub my $thread = threads->new( \&inc, $i, 2 ); } } while ( scalar threads->list() ) { sleep 1 } # I need access to the hash that's updated in the sub print Dumper \%count; sub inc { my ( $param1, $param2 ) = @_; #long subroutine using passed params # pausing for effect! # this simulates that the sub takes a while to run sleep( 5 + ( rand 5 ) ); # let you know something is happening print "$param1\n"; # make a change to the hash $count{$param1} = $param1 * $param2; threads->self()->detach; }