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
};
...
-
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.
|