Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

sharing array of arrays between threads

by bebe (Novice)
on Aug 10, 2012 at 04:51 UTC ( [id://986658]=perlquestion: print w/replies, xml ) Need Help??

bebe has asked for the wisdom of the Perl Monks concerning the following question:

Hi I'm trying to share an array of arrays between threads and I have no luck with that.
I'm new to Perl, I would really appreciate all your help
here is how I'm sharing array X


my $ARRAY_SIZE = 45;
my $X = &make_shared_array();

sub make_shared_array {
my $a = &share([]);
for (my $i = 0;$i < $ARRAY_SIZE;++$i) {
my $row = &share([]);
$a->[$i] = $row;
}
return $a;
}

sub abc {
push(@{$X[$var1]}, $Var2);
}


Basically, new string in (Var2) will be pushed into array X every time this sub gets called.
After we are done writing array X, I would like to be able to compare each of these arays X[0], X[1], X[2], .... Against a one_dim array "Y"
but before I even get to compare the arrays I tried to print X[1] and it's blank :(
I'm not sure what I'm missing.
Thank you so so much for your help


sub test{
Print "@X[1]\n";
}

This is what I'm hoping to print:


X[0] = ("QWE", "RTYU" ,"IOP")
X[1] = ("QSWE", "TYU" ,"IKOP", "BVC")
X[2] = ("QE", "TYU")
X[3] = ("QSWE", "TYU" ,"IKOP", "BVC", "LMNB")
.
.
.

Replies are listed 'Best First'.
Re: sharing array of arrays between threads
by aitap (Curate) on Aug 10, 2012 at 07:56 UTC
    One of your mistakes is that you first create $X which looks like it's a reference to array, but then try to print @X[1], which is an element of @X which is different to $X. See this example:
    $ perl -d -e 1 DB<1> $X = [ 1 .. 10 ]; # create a reference to an anonymous array DB<2> x $X # print the array by its reference 0 ARRAY(0x9a45510) 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 DB<3> @X = ( 2 .. 20 ); # create an array DB<4> x @X # display the array 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 13 12 14 13 15 14 16 15 17 16 18 17 19 18 20 DB<5> x @X[1] # display an element of the array 0 3 DB<6> x $X->[1] # display an element of the anonymous array by its r +eference 0 2
    See perlreftut for more information.
    Sorry if my advice was wrong.

      thank you so much for your help....
      I've also tried the following... but I still have no luck getting this to work


      my @X : shared;
      my $var = &share([]);
      for (my $i=0; $i< 222; $i++){
      $X[$i] = $var;
      }


      sub abc {
      my $Var2;
      .... Here the string in $Var2 gets updated everytime abc is called
      push(@{X[$i]}, $Var2);
      }


      thank you so much!!!

        How exactly this doesn't work? Did you try using Data::Dumper to print your variables inside the threads?
        Sorry if my advice was wrong.
Re: sharing array of arrays between threads
by kcott (Archbishop) on Aug 10, 2012 at 09:22 UTC

    Firstly, take a look at the following:

    In addition, here's a couple of points based on the code you've supplied:

    • You should generally be using subroutine() instead of &subroutine(), i.e. no leading ampersand (&). See perlsub.

      Update: See ++BrowserUk's comment below. threads::shared has:

      $var = &share([]); # Same as $var = shared_clone([]);
    • $a (and $b, which you haven't used) are special variables used by sort and a number of other list-related functions (see List::Util and List::MoreUtils for some examples). $a and $b are described in perlvar.

    -- Ken

      You should generally be using subroutine() instead of &subroutine(), i.e. no leading ampersand (&). See perlsub.

      Actually, if you read threads::shared, you'll find that his use of &share([]); is exactly the correct thing to do in this case.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        ++ Thanks for pointing that out. I've updated my node.

        -- Ken

Re: sharing array of arrays between threads
by rohit_raghu (Acolyte) on Aug 10, 2012 at 07:05 UTC
    what is share() ? Also, $ARRAY_SIZE is defined in the main package using 'my' and won't be accessible inside the subroutine, at-least I think so.

    Can you post your program as is? or atleast a trial version?

    Rohit Raghunathan

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://986658]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-19 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found