use threads;
use strict;
####
# I added this to make sure we see all printing on time
$| ++;
####
# constants
my $workers = 1; #number of simultaneous workers
workerPool();
sub list_threads {
my @threads = threads->list;
print "--threads i know of: -----\n";
for my $t (@threads) {
print $t->tid."\n";
}
print "--------------------------";
}
# sub which maintains a set number of simultaneous threads
sub workerPool {
print "Starting workerPool...\n";
my $count = 0;
my @running_threads = ();
while (1) {
@running_threads = threads->list;
if (scalar(@running_threads) < $workers) {
####
#I added this, and it only shows ONCE
print "creating new threads\n";
####
# add a new worker
$count++;
my $thread = threads->new(\&worker, $count);
}
####
# I commented out this, with this flushing the output all the time, I cannot see anything useful
#list_threads();
####
}
}
# this sub represents a thread
sub worker {
my $thread_num = $_[0];
print "---I am thread $thread_num!\n";
####
#this is simply a lie
print "+++Thread $thread_num gone!\n";
#this hangs this worker thread itself
eval(((threads->self)->join));
####
}