in reply to Re^4: forked::shared with hashes?
in thread forked::shared with hashes?

I just cited "the restriction" from the previous post. I thought the author might mean this:
In the standard Perl threads implementation, arrays and hashes are re-initialized when they become shared (with the share()) function. The share() function of forks::shared does not initialize arrays and hashes when they become shared with the share() function.
Quoted from the documentation of forks::shared.

Replies are listed 'Best First'.
Re^6: forked::shared with hashes?
by BrowserUk (Patriarch) on Apr 02, 2011 at 14:38 UTC
    I thought the author might mean this: ...

    Okay. I'm not sure that matches the OPs description: It turns out the problem has to do with the restriction that you should share scalar variables, not complex structures like hashes., but given that he is obviously very confused, and still not using strict, understandable :)

    You also said; "No, for me, it worked with a hash as well.". Does that work bidirectionally? Ie. Can the parent see changes made to the hash by the child?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Does that work bidirectionally?
      Yes, it does.

      UPDATE: Sample code:

      use warnings; use strict; use forks; use forks::shared; my %GLOBALHASH : shared; share (%GLOBALHASH); my $pid = fork (); if (!$pid) { # child $GLOBALHASH{OILPRICE} = 100; sleep 1; print "Gold price from the parent: $GLOBALHASH{GOLDPRICE}\n"; exit 0; } elsif ($pid) { # daddy $GLOBALHASH{GOLDPRICE} = 200; sleep 2; print "Global Oils Price is now: " . $GLOBALHASH{OILPRICE} . " dol +lars\n"; }
      Output:
      Gold price from the parent: 200 Global Oils Price is now: 100 dollars