Hi Monks, I have a hash I want to pass back from a thread via shared memory. Having done that for many years and quite reliably so with every other data type, this has me a little bit surprised. Note I have left out shared memory access synchronisation calls for simplicity in this example (I coordinate access via semaphores normally). In the code below, I am simply getting a "Magic number checking on storable string failed at /System/Library/Perl/5.18/darwin-thread-multi-2level/Storable.pm line 417, at ./test.pl line 46" error and it won't even run. Can anybody see the problem?
#!/usr/bin/perl -w use strict; use IPC::SysV qw(IPC_PRIVATE S_IRUSR S_IWUSR S_IRWXU IPC_CREAT IPC_EXC +L); use IPC::SharedMem; use Storable qw(freeze thaw); my %somehash; $somehash{'one'}{'count'} = 1; $somehash{'one'}{'string'} = "This is a big ol' string"; use constant { STOP => 0, RUN => 1 }; my $shm_mastercontrol = IPC::SharedMem->new(IPC_PRIVATE, 8, S_IRWXU); my $shm_somehash = IPC::SharedMem->new(IPC_PRIVATE, 1024, S_IRWXU); $shm_mastercontrol->write(pack("S", RUN), 0, 2); # forks processes my $thread_PID = fork(); if (not defined $thread_PID) { die "Insufficient resources to fork Runner!\n"; } elsif ($thread_PID == 0) { &thread(); } sub thread() { #my $string; while () { # increment the somehash counter: $somehash{'one'}{'count'}++; print "thread sees: $somehash{'one'}{'count'}\n"; my $serialized = freeze \%somehash; $shm_somehash->write(pack("A*", $serialized), 0, 1024); last if ( unpack("S", $shm_mastercontrol->read(0, 2)) == STOP); select(undef, undef, undef, 1.0); } exit(0); } while () { my $unpacked = unpack("A*", $shm_somehash->read(0, 1024)); # put somehash back from shared memory: %somehash = %{thaw($unpacked)}; print "main sees: $somehash{'one'}{'count'}\n"; print "press some key, q to quit:\n"; chomp (my $key = <STDIN>); if ( $key eq 'q' ) { $shm_mastercontrol->write(pack("S", STOP), 0, 2); waitpid($thread_PID, 0); last; } }

In reply to can't thaw a hash: Magic number error. by balt

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.