use strict; use threads; use threads::shared; use Win32::Console; our $N = 500; our $alldone : shared = 0; sub thread { my( $con, $n) = @_; my $tid = threads->self->tid; my( $y, $x ) = ( ( $tid % 20 ) * 5, int( $tid / 20 ) ); { lock $alldone; $alldone++; } $con->WriteChar( sprintf( '%04d', $_ ), $y, $x ) for 1 .. 3999; } $N = ($ARGV[0]=~/^-N=(\d+)/) ? $1 : 500; my $con = Win32::Console->new( STD_OUTPUT_HANDLE ); $con->Cls; my @threads = (); { lock $alldone; foreach (1.. $N) { push @threads, threads->new( \&thread, $con, 0 ) or die "threads->new failed with: $^E"; print "$_\r"; } print "\n"; } $_->join for @threads;