MultiThreading

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
 
R

Rainer Weikusat

Nitin Tripathi said:
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.
[...]

Can anyone please explain what is happening here.
[...]

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);
}

You've misunderstood the somewhat confusingly worded threads
manpage. Using threads::share does not imply that your variables
become shared but that you can declare variables which behave as if
they were shared[*] with the help of a :shared attribute, see 'perldoc
threads::shared' for more details.

[*] They really aren't. Each thread has its own copy of all of them
and the runtime keeps all the values in sync by copying data backwards
and forwards as necessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top