Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found