exilepanda has asked for the wisdom of the Perl Monks concerning the following question:
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 }; ...
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: use Devel::Pointer in threads
by LanX (Saint) on Apr 04, 2021 at 15:04 UTC | |
by exilepanda (Friar) on Apr 05, 2021 at 01:21 UTC | |
by LanX (Saint) on Apr 05, 2021 at 11:45 UTC | |
Re: use Devel::Pointer in threads
by 1nickt (Canon) on Apr 04, 2021 at 14:50 UTC | |
Re: use Devel::Pointer in threads
by LanX (Saint) on Apr 04, 2021 at 15:48 UTC | |
by exilepanda (Friar) on Apr 05, 2021 at 01:08 UTC | |
Re: use Devel::Pointer in threads
by vr (Curate) on Apr 05, 2021 at 13:07 UTC | |
Re: use Devel::Pointer in threads
by karlgoethebier (Abbot) on Apr 05, 2021 at 07:32 UTC |
Back to
Seekers of Perl Wisdom