Implicit sharing of nested structures is prohibited because it creates the potential for accidential sharing of private data. Since the ithreads model is predicated upon complete separation of all data by default, to allow the capacity to implicitly share references within shared parent structures is to open the door to accidental corruption of data. From perlthrtut

use threads; use threads::shared; my $var = 1; my $svar : shared = 2; my %hash : shared; ... create some threads ... $hash{a} = 1; # all threads see exists($hash{a}) and $hash{a} == + 1 $hash{a} = $var # okay - copy-by-value: same effect as previous $hash{a} = $svar # okay - copy-by-value: same effect as previous $hash{a} = \$svar # okay - a reference to a shared variable $hash{a} = \$var # This will die delete $hash{a} # okay - all threads will see !exists($hash{a})

So the solution using threads is to take references to the things you wish to share at each level of a parent structure and to share them on a case by case basis. In other words, you must explicitly share not only the parent reference, but every reference contained therein.

Here's some example code of a basic object with shared members:

use strict; use warnings; package Foo; sub new { my ($class, $arg) = @_; my $this = bless {}, $class; $this->{args} = undef; return $this; } sub set { my ($this, $arg) = @_; $this->{args}[0] = $arg; # setting an entry in a shared array refe +rence } 1; # End of the module, and now a test script use strict; use warnings; use Foo; use threads; use threads::shared; my $foo = new Foo(); my $nested_array = []; my $nested_string = 'bar'; share($foo); share($nested_array); share($nested_string); $foo->{args} = $nested_array; # set the shared array reference # pass in a reference to the shared scalar my $thr1 = threads->create(sub { $foo->set(\$nested_string) }); <Update> # If in Foo::set we manually set the argument passed, say, to 'quux', # the object will contain that string rather than 'bar', # proof that we do indeed have a shared nested reference. </Update> $thr1->join(); print $foo->{args}[0];

It's a bother to do this, but it's better than accidental trampling of data. Hope this helps.


In reply to Re: How to share huge data structure between threads? by djantzen
in thread How to share huge data structure between threads? by ph0enix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.