use strict; use warnings; use threads; use threads::shared; # enable autoflush on the handle STDERR->autoflush; my $sem_stderr :shared; sub print_err { lock $sem_stderr; print STDERR @_; } sub task { my $id = shift; my $str = "ab " x 3000; for (1..10000) { print_err($str, "\n"); } } threads->create('task', $_) for 1..40; $_->join for threads->list;