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


In reply to thread::shared - shared but not shared? by learnedbyerror

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.