PUB /
PerlThreadingCode
use strict;
use warnings;
use threads;
use threads::shared;
use Config;
my $threads : shared = 0;
my @threads;
foreach my $x (1 .. 20) {
my $thread = threads->new(\&start_thread, int(rand(10))+1 );
push(@threads, $thread);
}
# my @list = threads->list();
# print join("\n", @list)."\n";
# foreach my $thread (@threads) {
# print $thread->tid() . "\n";
# }
print threads->self->tid() ."\n";
while ($threads) {
sleep(1);
}
1;
sub start_thread {
my $opt = shift;
{
lock($threads);
$threads++;
}
print "I am thread ".threads->self->tid()." of $threads, I was started with option $opt\n";
sleep($opt);
{
lock($threads);
$threads--;
}
}
BEGIN {
$Config{useithreads} or die "Recompile Perl with threads to run this program.";
print "Once upon a time...\n";
}
END {
print "The End.\n";
}
__END__