[32768] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4032 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Sep 12 05:17:43 2013

Date: Thu, 12 Sep 2013 02:17:04 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 12 Sep 2013     Volume: 11 Number: 4032

Today's topics:
        MultiThreading <2nitintr@gmail.com>
    Re: MultiThreading <rweikusat@mobileactivedefense.com>
    Re: Why can't I use "say" with version 5.14 <jblack@nospam.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Wed, 11 Sep 2013 07:30:28 -0700 (PDT)
From: Nitin Tripathi <2nitintr@gmail.com>
Subject: MultiThreading
Message-Id: <42fd60d6-89f6-479d-bedb-b1065eafda3e@googlegroups.com>

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


------------------------------

Date: Wed, 11 Sep 2013 15:50:50 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: MultiThreading
Message-Id: <878uz3o1it.fsf@sapphire.mobileactivedefense.com>

Nitin Tripathi <2nitintr@gmail.com> 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.


------------------------------

Date: Wed, 11 Sep 2013 11:04:55 -0500
From: John Black <jblack@nospam.com>
Subject: Re: Why can't I use "say" with version 5.14
Message-Id: <MPG.2c9a5088bb674179989789@news.eternal-september.org>

In article <jq54ga-asl1.ln1@anubis.morrow.me.uk>, ben@morrow.me.uk says...
> 
> Quoth John Black <jblack@nospam.com>:
> > perl -v reveals that I have version 5.14 installed but the "say" command
> > does not work unless 
> > I include this line:
> > 
> > use feature ':5.10';
> > 
> > Shouldn't 5.10 features already be default in 5.14??
> 
> No. That's the whole point of feature.pm: you might already have a
> pre-5.10 program which uses a 'say' sub, and if 5.10 (or 5.14) turned
> the builtin on by default that program would stop working.
> 
> From 5.10 onwards, 'use 5.010' will call 'use feature ":5.10"' in
> addition to the compile-time check for perl 5.10. The same applies for
> later versions of perl. This means you can put 'use VERSION' at the top
> of each file along with strict and warnings and that file will continue
> to use the semantics of the version you wrote it for even on future
> versions of perl.
> 
> (Incidentally, 'use 5.012' and later also imply 'use strict'...)
> 
> Ben

Thanks!

John Black


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 4032
***************************************


home help back first fref pref prev next nref lref last post