I am writing a multithreaded program (say, large number of threads just to make the problem dramatic) where I need multiple threads to change hash values of hash keys in an atomic fashion. I understand that I can use shared variables and lock etc, but is there a way to lock just one key instead of the entire hash structure? It seems odd that there is not a straight forward way to lock just the key I am operating on, but have to lock the entire structure. What am I missing here?
our %h:shared; sub lock { my ($key, $val) = @_; # critical section { lock($h); # don't like this part, I want to lock($h{$key}); $h{$key} = $val; } }
In reply to Avoid Locking Entire Hashes by jagan_1234
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |