Quoth the CPAN:
share() allows you to share($hashref->{key}) and share($arrayref->[idx]) without giving any error message. But the $hashref->{key} or $arrayref->[idx] is not shared, causing the error "lock can only be used on shared values" to occur when you attempt to lock($hashref->{key}) or lock($arrayref->[idx]) in another thread.
I can't say how effective this is, but the following tweak gets a local direct reference to the shared hash instead of following a dereference in the lock call, and doesn't complain.
Gives:use strict; use threads; use threads::shared; use warnings; sub lockit($) { my $uber_hashRef = shift ; print "Yey\n" if is_shared( $$uber_hashRef{'%y'} ) ; my $subHash = $uber_hashRef->{'%y'}; lock ($subHash) ; print "Done!\n"; } sub genSharedLower($) { my $hashRef = shift ; $$hashRef{'%y'} = &share({}) ; } my %all_shary_for_subs : shared ; genSharedLower(\%all_shary_for_subs); print "Yey\n" if is_shared( $all_shary_for_subs{'%y'}); lockit( \%all_shary_for_subs) ;
C:\Documents and Settings\nd250020\Desktop\dev>perl test.pl Yey Yey Done!
In reply to Re: PERL issues a "lock can only be used on shared values" when locking a shared hash
by SuicideJunkie
in thread PERL issues a "lock can only be used on shared values" when locking a shared hash
by ISAI student
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |