Tyr has asked for the wisdom of the Perl Monks concerning the following question:

I don't know how to get informations from my objects array with threads::shared; Show you, a piece of code :

my @map; sub init_map { my ($X, $Y) = @_; for ($i = 0; $i < $Y; $i++) { for ($j = 0; $j < $X; $j++) { $map[$i][$j] = Case->new(); } } } sub events_callbacks { # my SDL events callbacks # display informations from @map # I don't know how to access the @map array # ... } sub launch { init_map 60, 60; # I have to get informations from @map In this func # This is $thr = threads->new(\&events_callbacks); # In this function I communicate with a server # and modify informations from @map talk; $thr->join; }
Thank You !

Replies are listed 'Best First'.
Re: How to share an objects array between threads ?
by Tyr (Initiate) on Jun 19, 2010 at 12:34 UTC
    up

      In essence, all you need is:

      use threads::shared; my @map :shared; ...

      But then, you need to use locking to prevent corruption through concurrent updates. And you'll also need to ensure that your objects are compatible with threading.

      All of this is documented in the module you mentioned: threads::shared. So rather than expecting us to reiterate all the information that you already have at your finger tips, you'd be more likely to get a response if you told us what you don;t understand; or better yet, what you've had trouble with implementing.

      As it is, you don't appear to have made any attempt to use the module you've pointed out you need.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.