MultiThreading

Discussion in 'Perl Misc' started by Nitin Tripathi, Sep 11, 2013.

  1. 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
    Nitin Tripathi, Sep 11, 2013
    #1
    1. Advertising

  2. Nitin Tripathi <> writes:
    > 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.
    Rainer Weikusat, Sep 11, 2013
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Boris Condarco

    Multithreading

    Boris Condarco, Jun 28, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    598
    Boris Condarco
    Jun 30, 2003
  2. Ilia
    Replies:
    6
    Views:
    3,002
    John Saunders
    Nov 4, 2003
  3. Fieldadvice

    Multithreading with HttpWebRequest

    Fieldadvice, Nov 6, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    1,334
  4. Muralidharan Ramakrishnan

    MultiThreading

    Muralidharan Ramakrishnan, Nov 10, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    411
    Kevin Spencer
    Nov 10, 2003
  5. BoomWackaBoom

    Asynchronous requests vs. multithreading

    BoomWackaBoom, Nov 14, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    631
    Alvin Bruney
    Nov 15, 2003
Loading...

Share This Page