Re: (forks) When do I need to lock variables?
[email protected] (Elizabeth Mattijsen)
| Newsgroups | perl.ithreads |
|---|---|
| Message-ID | <p05111b0bbb95f5b52ef2@[192.168.56.4]> |
At 13:52 +0200 9/23/03, Per Andreas Buer wrote:
>I've playing around with forks and forks::shared for some time now - it
>looks really nifty. In the past I've been spending too much time
>emulating threads in Perl. :/
>I am not quite sure when I need to lock. Is "$HASH{foo}++" safe?
In general, increment on a shared variable is not safe. It's safe in
the sense that Perl won't crash. It's _not_ save in the sense that
you might miss increments. This is because shared variables in Perl
are implemented as tied variables, i.e. with a FETCH and STORE. An
increment is this basically a FETCH, an increment and a STORE. If
second thread does a FETCH before the first one has done the STORE,
you have lost 1 increment.
> How
>about "$foo = scalar keys %HASH"? Should a wrap access to shared lists
>and hashed with tie and lock there?
I'm not sure what you mean here...
Liz