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:
Output: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__
$ 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!
In reply to Re^3: Shared memory and asynchronous access
by 1nickt
in thread Shared memory and asynchronous access
by vef445
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |