use threads; ... test_threads(); sub test_threads { my $start = time; my @thrs; # must save threads to join later push @thrs, threads->create('work') for (1..$num_workers); # wait for threads to finish $_->join for @thrs; # close the shared file handle, flushes buffer $iter_o->close(); printf STDERR "testa done in %0.02f seconds\n", time - $start; }