in reply to Re: How to share complex data structure in threads ?
in thread How to share complex data structure in threads ?
Thanks for the lead. For the sub{} share stuff, I am still not sure it's necessity. But consider the following code,
If I give anything try to alter the cloned structure, it give the Thread 2 terminated abnormally: Invalid value for shared scalar error.
This kind of assessment ( shareable R/W stuct ) would be more prior for my needs.$| = 1; use Data::Dumper; use threads;; use threads::shared; my @r = ( map+{ map{ $_ => [ 1 .. 2 ] } 'a' .. 'b' }, 1..2 );; my @s : shared = map shared_clone( $_ ), @r; sub A { while ( 1 ) { print Dumper \@s; sleep 3; } } sub B { while ( 1 ) { my $cmd = <STDIN>; chomp $cmd; push @s, { $cmd => time } } } $th1 = threads -> new ( 'A' ) ; $th2 = threads -> new ( 'B' ) ; $th1->join(); $th2->join();
And for my ultimate purpose, I wish to share a struct which carrying different on-the-fly created objects, and they can cross access each other. Of cause I can change the style to share an array to indicate a "queue" and then every objects go inside the queue to look for action. However, this leads me to think, is if there's any transparent way for a complex stuct become shareable
In Section
Seekers of Perl Wisdom