#!/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; for my $i (0..5) { $sem->down; my $t = threads->create(\&mySubName, $i); } <>; print join(" ",@results); <>; sub mySubName { threads->detach(); my $foo = shift(@_); print "$foo\n"; lock @results; push (@results,$foo); $sem->up; }