use strict; use warnings; use Config; BEGIN { $Config{useithreads} or die ('Recompile Perl with threads to run this program.'); }; use threads; my $i = 0; my $size = 200; my $start_byte = 0; my @THRD_LIST; my $thrd; my $targetSize = $size / 10; while ($size) { my $chunk = $targetSize; $i++; $chunk = $size if $chunk > $size; $size -= $chunk; print "Process part $i. Size is $chunk\n"; $thrd = threads->create (\&transfer_chunks, $i, $chunk) or die "Failed to start the thread: $@\n"; push (@THRD_LIST, $thrd); } # start the threads for $thrd (@THRD_LIST) { $thrd->join (); } print "All done\n"; sub transfer_chunks { my ($i, $chunk) = @_; while ($chunk--) { print "$i "; sleep 1 + rand (3); } print "\nprocess $i complete\n"; } #### Process part 1. Size is 4 1 Process part 2. Size is 4 Process part 3. Size is 4 2 Process part 4. Size is 4 3 Process part 5. Size is 4 4 Process part 6. Size is 4 5 Process part 7. Size is 4 6 Process part 8. Size is 4 7 Process part 9. Size is 4 8 Process part 10. Size is 4 9 10 1 3 4 5 6 10 3 4 8 9 2 3 6 7 8 10 1 5 7 8 9 process 3 complete 4 10 2 process 4 complete 6 1 5 7 process 8 complete 9 2 process 6 complete process 9 complete process 10 complete process 1 complete process 5 complete process 7 complete process 2 complete All done