learnedbyerror has asked for the wisdom of the Perl Monks concerning the following question:
Oh Monks, I bring to you a riddle. When is_shared, not shared?
I have a threaded program in which I create a shared hash in main:
use threads qw(stringify); use threads::shared; my %contacts : shared;
I load a hashref into the hash in a sub-routine sharing each level as such:
$contacts{$contact} = &share( {} ); share( $contacts{$contact}{'a'} ); share( $contacts{$contact}{'inc_dnd'} ); share( $contacts{$contact}{'lock'} ); $contacts{$contact}{'a'} = 1; $contacts{$contact}{'inc_dnd'} = &share( {} ); $contacts{$contact}{'lock'} = 0;
In a thread, I try to execute
lock( $contacts{$user_id} );I receive the following error
lock can only be used on shared values at get_contacts_pool_photos.pl line 170.However, if I run the program in the debugger I can verify that I can access the hash and that it contains the data that I expect
DB<2> print Dumper $contacts{$user_id} $VAR1 = { 'inc_dnd' => {}, 'a' => '1', 'lock' => '0', 'd' => '/tmp' };
Futhermore, if I check to see if the variable is shared, I find that it is.
170: lock( $contacts{$user_id} ); DB<2> print is_shared($contacts{$user_id}) 226839368 DB<3> n lock can only be used on shared values at get_contacts_pool_photos.pl +line 170. at get_contacts_pool_photos.pl line 170 main::get_contacts_pool_photos() called at get_contacts_pool_p +hotos.pl line 93
So Monks, I ask, is it shared or not?
How should I fix my problem?
Thanks in advance for your guidance.
lbe
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: thread::shared - shared but not shared?
by BrowserUk (Patriarch) on Sep 25, 2011 at 18:49 UTC | |
|
Re: thread::shared - shared but not shared?
by zentara (Cardinal) on Sep 25, 2011 at 19:21 UTC |