La version 5.8 se voit lotie d'une nouvelle implémentation du multi-threading. Celui-ci s'appuie sur les ithreads.
Cette version n'est pas compilée par defaut avec Perl, il convient de re-compiler Perl après avoir lancé ./Configure et avoir volontairement activé le multi-threading.
La version 5.8.0 n'implémente pas une version stable du multi-threading, le programme donné en exemple va enfler en mémoire de façon absolument anormale. N'utilisez pas le multi-threading en production, c'est pour le moment un exercice de style.
#!/usr/local/bin/perl
use strict;
use threads;
use threads::shared;
my %free;
share(%free);
my $th = 0;
my $max_th = 10;
my $total_th = 100;
my $actual = 0;
my $rand = 20;
sub count_pl
{
my $max = shift;
my $i = shift;
for(my $i = 1; $i <= $max; $i++)
{
# printf("%d => %d\n",$$,$i);
sleep(1);
}
undef($free{$i});
return $max;
}
my @t;
for(my $i = 0; $i <= $max_th; $i++)
{
$free{$i} = 1;
$th++;
undef($t[$i]);
$t[$i] = threads -> create(\&count_pl,int(rand($rand)),$i);
$actual++;
printf("Creation of a new thread %d\n",$t[$i] -> tid);
}
while($th > 0)
{
for(my $i = 0; $i <= $max_th; $i++)
{
if(not defined($free{$i}))
{
my $value = $t[$i] -> join();
printf("(%d) Thread %d returned %s\n",$th,$i,$value);
$free{$i} = "0";
$th--;
if($actual <= $total_th)
{
$t[$i] = threads -> create(\&count_pl,int(rand($rand)),$i);
$th++;
$free{$i} = 1;
$actual++;
printf("Creation of a new thread %d position # %d\n",$t[$i] -> tid,$i);
}
}
}
sleep(1);
}
Plus d'informations dans les pages de man