#! perl -slw use strict; use threads; use threads::shared; my $sem :shared; our $MAX //= 10; my $array = 'list.txt'; open my $handle, '<', $array; chomp(my @array = <$handle>); close $handle; my @threads; for my $printme ( @array ) { push @threads, async{ lock $sem; print $printme; }; sleep 1 while threads->list( threads::running ) > $MAX; } $_->join for @threads; #### #! perl -slw use strict; use threads; use threads::shared; use Thread::Queue; our $MAX //= 10; my $sem: shared; my $Q = new Thread::Queue; sub thread { while( my $printme = $Q->dequeue ) { lock $sem; print $printme; } } my @threads = map async( \&thread ), 1 .. $MAX; my $array = 'list.txt'; open my $handle, '<', $array; chomp, $Q->enqueue( $_ ) while defined( $_ = <$handle> ); close $handle; $Q->enqueue( (undef) x $MAX ); $_->join for @threads;