Generally, you would declare the shared variable in a script then fork a worker or workers to do the separate task, where they would have access to the shared var.

Update: Here's a very simple demo:

use strict; use warnings; use feature 'say'; use Time::HiRes qw/ time usleep /; use MCE::Shared; say sprintf '%-5s %-17s %-s', 'PID', 'Time', 'Action'; my $shared = MCE::Shared->condvar(0); say sprintf '%s %.06f Init val: %s', $$, time, $shared->get; my $pid = fork; if ( $pid ) { for ( 0 .. 19 ) { say sprintf '%s %.06f write: %s', $$, time, $shared->incr; usleep 10000; } } else { while (1) { usleep 50000; say sprintf '%s %.06f READ: %s', $$, time, $shared->get; } } __END__
Output:
$ perl shared.pl PID Time Action 16648 1491411785.712927 Init val: 0 16648 1491411785.713604 write: 1 16648 1491411785.724157 write: 2 16648 1491411785.734731 write: 3 16648 1491411785.745270 write: 4 16648 1491411785.755807 write: 5 16650 1491411785.763780 READ: 5 16648 1491411785.766309 write: 6 16648 1491411785.776740 write: 7 16648 1491411785.787272 write: 8 16648 1491411785.797800 write: 9 16648 1491411785.808273 write: 10 16650 1491411785.814272 READ: 10 16648 1491411785.818806 write: 11 16648 1491411785.829300 write: 12 16648 1491411785.839957 write: 13 16648 1491411785.850539 write: 14 16648 1491411785.861082 write: 15 16650 1491411785.864522 READ: 15 16648 1491411785.871574 write: 16 16648 1491411785.882031 write: 17 16648 1491411785.892612 write: 18 16648 1491411785.903151 write: 19 16648 1491411785.913714 write: 20 16650 1491411785.914756 READ: 20

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re^3: Shared memory and asynchronous access by 1nickt
in thread Shared memory and asynchronous access by vef445

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.