#!/usr/bin/perl use strict; use warnings; use Config; use threads; use Time::HiRes qw( usleep ); $Config{useithreads} or die('Recompile Perl with threads to run this program.'); foreach my $i (1 .. 100000) { while ((my $nthreads = scalar threads->list()) >= 10) { print $nthreads . " running. waiting 1 second.\n"; sleep(1); foreach my $thread (threads->list(threads::joinable)) { $thread->join(); } } threads->create(\&sub1); usleep(333333); foreach my $thread (threads->list(threads::joinable)) { $thread->join(); } } foreach my $thread (threads->list()) { $thread->join(); } sub sub1 { my $sleep_value = int(rand(2) * 100) / 100; print "thread id " . threads->tid() . ": Start sleeping " . $sleep_value . " seconds\n"; usleep($sleep_value * 1000000); print "thread id " . threads->tid() . ": Done sleeping " . $sleep_value . " seconds\n"; }