I know thread::shared can't share deep data structure, so I attempt to only share the stringify memory address, and deref() it to access the structure inside the child thread. But what I got is really weird. Can someone help to explain how this happen? and any fix for this approach? Thank you very much
$| = 1;
use threads;
use threads::shared;
use Devel::Pointer;
use Data::Dumper; $Data::Dumper::Sortkeys;
my $Addr :shared;
my $Deep = {OK=>1};
$Addr = address_of $Deep;
sub A {
while ( 1 ) {
my $Data = deref $Addr;
$Data->{A}++;
print "Dumper in A tells:";
print Dumper $Data;
print "B is now '$Data->{B}'"; # nothing!
$Data->{B} = "Overide by A"; # The output suprised me!
print $/; sleep 2;
}
}
sub B {
sleep 1;
while ( 1 ) {
my $Data = deref $Addr;
$Data->{B}++;
print "Dumper in B tells:";
print Dumper $Data;
print "A is now '$Data->{A}'";
print $/; sleep 2;
}
}
my $A = threads -> create ( 'A' ) ;
my $B = threads -> create ( 'B' );
$A->detach; $B->detach;
while ( 1 ) { $Addr = address_of $Deep ; sleep 1 }
__END__
Surprising Output:
Dumper in A tells:$VAR1 = {
'A' => 1,
'OK' => 1
};
B is now '' ## Nothing!
Dumper in B tells:$VAR1 = {
'A' => 1,
'B' => 1,
'B' => 'Overide by A', ## POSSIBLE?! Same key in Hash?!
'OK' => 1
};
A is now '' ## Nothing too!
Dumper in A tells:$VAR1 = {
'A' => 2,
'B' => 1,
'B' => 'Overide by A',
'OK' => 1
};
...
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.