my $thread_term :shared = 0; my $threads_init :shared = 0; my $file_read_sem : shared; ### Add this ... WORKER: while ( !$thread_term && defined ( my $file = $work_queue->dequeue ) ) { { ### Add this lock $file_read_sem; ### Add this open my $fh, '<', $file or do { lock $d_counter; $d_counter++; next WORKER }; my $data = <$fh>; close $fh; } ### Add this