#!/usr/bin/perl -w use strict; use threads; use Thread::Semaphore; my $sem = Thread::Semaphore->new(15); # max 15 threads my @threads; for my $i (0..5) { $sem->down; my $t = threads->create(\&mySubName, $i); push(@threads,$t); } foreach my $t (@threads) { print $t->join(),"\n"; } sub mySubName { my $foo = shift(@_); return $foo * 10; # release slot: $sem->up; } #### ##!/usr/bin/perl -w use strict; use threads; use threads::shared; use Thread::Semaphore; my $sem = Thread::Semaphore->new(15); # max 15 threads my @results :shared = 1; for my $i (0..5) { $sem->down; my $t = threads->create(\&mySubName, $i); } print join(" ",@results); sub mySubName { threads->detach(); my $foo = shift(@_); push (@results,$foo); $sem->up; }