These examples explained everything that I was missing.push @s, &share( { $cmd => time } }; ## The & before share() is import +ant here; and one of the few times you should ever use it. ## or push @s, &share_clone( { $cmd => time } }; ## or my %hash : shared = ( $cmd => time() ); push @s, \%hash;my @array : shared; $array[ 0 ] = 'fred'; ## ok $array[ 1 ] = 12345; ## ok $array[ 2 ] = [ 1,2,3 ] ## NOT OK $array[ 2 ] = &share( [ 1,2,3 ] ); ## OK $array[ 3 ] = { 'a'..'z' }; ## NOT OK my %hash : shared = ( 'a' .. 'z' ); $array[ 3 ] = \%hash; ## OK
My real project is hard to explain, as the project scope is still defining, but from what I can understand, the structure is sound like a "online ARPG game". So, there's a World Map, and there's monsters, and players.
So, whenever a player is connected, a new thread created, while everyone will access the same world map. When players walk, they may able to see monster or other players. and their coords changed, which should able to reflect to other players. And by certain time, killed monsters will reborn.$World = { Players => [ "player1" => { # each key is a player id ID=>"player1", HP=>9999, MP=>2000, coX=>0, coY=>0, Equip => [ qw/xxxSword yyyShield/ ], Magic => [ qw/Earthquake/ ], Invent => { Cure => 1, Poison => 3 } }, "playerx" => { ... } ], Monsters=>{ MonsterA => [ "coX1-coY1", "coX2-coY2", ... ] MonsterB => [ "coX1-coY1", "coX2-coY2", ... ] } };
This is almost the case I am likely to face. Indeed, this is more safe in the "queue" approach, but on the other side, I believe I will create more bugs while coding as the full picture is harder to retrieve.
However one situation is true here which is fuzzy condition acceptable. Glitch from time race is acceptable in a certain amount of range.
Indeed, I still can't decide which approach is better for me
In reply to Re^4: How to share complex data structure in threads ?
by exilepanda
in thread How to share complex data structure in threads ?
by exilepanda
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |