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.
$| = 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();
This kind of assessment ( shareable R/W stuct ) would be more prior for my needs.
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
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, 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, 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.
|
|