use threads; use threads::shared; my %thread_state:shared; # ignore the creation of threads # a loop that collects all threads while (1) { foreach (threads->list()) { if ($thread_state{$_->tid} eq 'finished') {$_->join;} } sleep 1; last if (&all_finished); } # the thread body looks like this sub thread_body{ # do something # when finished, mark itself { lock %thread_state; $thread_state{$threads->tid} = 'finished'; } }