in reply to Re: threads::shared - when to lock a hash ?
in thread threads::shared - when to lock a hash ?
You always have to lock shared variable when you accessing itWrong. Perl internally locks the variable before reading from or writing to it, so you don't need to lock it to avoid a corrupted value.
As to the OP's questions: R1 is safe, while R2 behaves like R3: i.e. it iterates over the hash pushing individual values onto @x. In R3, if another thread is adding or deleting elements, or also iterating, then it will affect the result in the same way as if a single thread was doing it, e.g.
W1,2,3 are all safe.while (($k, $v) = each(%h)) { # stuff here which adds or deletes elements, # or uses each/keys/values }
Of course in all the above, "safe" means "not corrupting perl's internal state"; perl's internal locking causes all the reads and writes to be serialised. You may of course still need to do locking at a higher level to ensure the safety of your own code's logic
Dave.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: threads::shared - when to lock a hash ?
by zwon (Abbot) on Oct 16, 2011 at 14:26 UTC | |
by dave_the_m (Monsignor) on Oct 17, 2011 at 06:53 UTC | |
|
Re^3: threads::shared - when to lock a hash ?
by ikegami (Patriarch) on Oct 17, 2011 at 03:43 UTC | |
by dave_the_m (Monsignor) on Oct 17, 2011 at 06:47 UTC | |
by ikegami (Patriarch) on Oct 17, 2011 at 06:57 UTC | |
by choroba (Cardinal) on Oct 17, 2011 at 10:30 UTC |