in reply to Accessing class data from a second thread

you have to create the variable as shared using the shared attribute, in the form of

my $CurrentTagList : shared = TagList->new();

Also within the Taglist class you have to use threads::shared's bless function so blessing propagates across all threads

Replies are listed 'Best First'.
Re^2: Accessing class data from a second thread
by Anonymous Monk on Jun 21, 2011 at 02:25 UTC

    I'm stuck with "Also within the Taglist class you have to use threads::shared's bless function so blessing propagates across all threads".

    Using:

    my $CurrentTagList : shared = TagList->new();

    initially returned an error but after changing the constructor for TagList to:

    package TagList; sub new { my $class = shift; my @self : shared = @_; my _TagList = [], return bless \@self, $class; }

    this at least executes but fails when

    my $array = $self->{'_TagList'};

    executes inside a sub which is part of the TagList class with the error "Not a HASH reference".

    I'm guessing my error still lays in the constructor but I'm at a lost there.

    Thoughts??

    Thanks for all your help!

Re^2: Accessing class data from a second thread
by Anonymous Monk on Jun 20, 2011 at 18:13 UTC

    Thank you very much for your response.

    I followed (and have tried previously) your first comment but get the error: "Invalid value for shared scalar".

    Now I'm assuming the second part of what you said will resolve this but I don't quite follow. Can you please clarify what I need to do inside the class?

    Thanks again!

      Are you assigning a hash or array reference to a shared scalar?

      If so, be sure to share the hash/array before taking the reference.

        I am assigning an instance of a class to the shared scalar.