in reply to OO and Threads conflict?

I'd hazard a guess that it's the difference between sharing a variable and sharing a value, where in the former case you've actually got a label by which to refer to a thing and in the latter you're doing a hash lookup and returning a string or integer or something. Does it work if you say  $self->{var} = $var; $self->{var} : shared = 1; ?

Replies are listed 'Best First'.
Re: Re: OO and Threads conflict?
by Anonymous Monk on Sep 20, 2002 at 22:15 UTC
    Nope...

    $self->{var} : shared = 1; gives a syntax error. Doesn't matter what you write before it. It's still a syntax error.

      What about declaring the variable shared before assigning it to your hash, as in my $var : shared = 1; $self->{var} = \$var. Although on further consideration I wonder if it might be better to be using a package variable via our or use vars since it looks like what you want is a 'static' variable i.e., one shared by all instances of a class, and these constructs provide that behavior. I don't have threads compiled so I can't test this, but this might work:

      package OOThread; use threads; our static_var : shared = 1; # may not be necessary to explicitly shar +e 'our' variables sub new {...} ...

        About that...

        I absolutely, positively and lifethreateningly do NOT want to share a variable between all instances of the class.

        I want to share the variable between two threads, both running in the same object.

        Sharing between threads, not between objects. Vewy vewy impowtant. =) By default threads will get their own copy of all the data (a la fork) except for those vars that are " : shared". =)