N
Nitin Tripathi
Hello All,
Here is a sample program, where i am trying to learn the MultiThreading.
But i dint understand, when i started 2 threads and called sub1 passing ArrayOfHashofHash A as an argument.
-- sub1 function assigns data in ArrayofHashofHash.
-- printA function prints the ArrayofHashofHash.
when i call the printA in sub1, it prints the data but when it is called after the joining the threads, it doesnt prints anythings.
So It seems that, ArrayofHashofHash is getting destroyed.
Can anyone please explain what is happening here.
Thanks,
Nitin Tripathi.
Sample.pl
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;
print "Starting main program\n";
my @threads;
my @A;
my $i;
for ( my $count = 1; $count <= 2; $count++)
{
my $t = threads->new(\&sub1, $count, \@A);
push(@threads, $t);
#printA( \@A);
}
foreach (@threads) {
my $num = $_->join;
print "done with $num\n";
}
print "End of main program\n";
printA( \@A);
sub printA {
print "Printing A\n";
my $AoHoH = shift;
foreach $i ( @{ $AoHoH } )
{
foreach $a ( keys %{ $i } )
{
foreach $b ( keys %{ $i->{$a} } )
{
#print $i->{$a}{$b}, " " ;
print $i->{ $a }{$b}, " " ;
}
}
print "\n" ;
#print $i->{1}{90}, " " ;
}
return 0;
}
sub sub1 {
my $i;
my $num = shift;
my $AoHoH = shift;
my %HoH;
foreach $i ( 90 .. 100)
{
$HoH{$num}{$i} = $i ;
}
print "started child process for $num\n";
#sleep $num;
push @{ $AoHoH }, \%HoH;
print "done with child process for $num\n";
printA( $AoHoH ) ;
return $num;
}
Sample.output - Starts
illin304!nitintr:~/RND/TRAINING/PERL/OOP [1119]$ ./thread_array_bkp.pl
Starting main program
started child process for 1
done with child process for 1
Printing A
90 95 97 94 91 92 99 98 93 100 96
started child process for 2
done with child process for 2
Printing A
90 95 97 94 91 92 99 98 93 100 96
done with 1
done with 2
End of main program
Printing A
Sample.output - Ends
--NITIN TRIPATHI
I was Expecting It should have printed the
End of main program
Printing A
90 95 97 94 91 92 99 98 93 100 96
Here is a sample program, where i am trying to learn the MultiThreading.
But i dint understand, when i started 2 threads and called sub1 passing ArrayOfHashofHash A as an argument.
-- sub1 function assigns data in ArrayofHashofHash.
-- printA function prints the ArrayofHashofHash.
when i call the printA in sub1, it prints the data but when it is called after the joining the threads, it doesnt prints anythings.
So It seems that, ArrayofHashofHash is getting destroyed.
Can anyone please explain what is happening here.
Thanks,
Nitin Tripathi.
Sample.pl
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;
print "Starting main program\n";
my @threads;
my @A;
my $i;
for ( my $count = 1; $count <= 2; $count++)
{
my $t = threads->new(\&sub1, $count, \@A);
push(@threads, $t);
#printA( \@A);
}
foreach (@threads) {
my $num = $_->join;
print "done with $num\n";
}
print "End of main program\n";
printA( \@A);
sub printA {
print "Printing A\n";
my $AoHoH = shift;
foreach $i ( @{ $AoHoH } )
{
foreach $a ( keys %{ $i } )
{
foreach $b ( keys %{ $i->{$a} } )
{
#print $i->{$a}{$b}, " " ;
print $i->{ $a }{$b}, " " ;
}
}
print "\n" ;
#print $i->{1}{90}, " " ;
}
return 0;
}
sub sub1 {
my $i;
my $num = shift;
my $AoHoH = shift;
my %HoH;
foreach $i ( 90 .. 100)
{
$HoH{$num}{$i} = $i ;
}
print "started child process for $num\n";
#sleep $num;
push @{ $AoHoH }, \%HoH;
print "done with child process for $num\n";
printA( $AoHoH ) ;
return $num;
}
Sample.output - Starts
illin304!nitintr:~/RND/TRAINING/PERL/OOP [1119]$ ./thread_array_bkp.pl
Starting main program
started child process for 1
done with child process for 1
Printing A
90 95 97 94 91 92 99 98 93 100 96
started child process for 2
done with child process for 2
Printing A
90 95 97 94 91 92 99 98 93 100 96
done with 1
done with 2
End of main program
Printing A
Sample.output - Ends
--NITIN TRIPATHI
I was Expecting It should have printed the
End of main program
Printing A
90 95 97 94 91 92 99 98 93 100 96