my $finished = 0 : shared; sub write_to_file { until ( $finished ) { open ( my $temp_fh, ">>", $temp_file ); print $temp_fh "aabbcc\n"; close ( $temp_fh ); } } #main bit of code here... $finished = 1; foreach my $thread ( threads -> list() ) { $thread -> join(); }