Hi,

I am trying to integrate IPC::Shareable module to Parallel::Forkmanager module, but having troubles with global variables.

I have developed a proof of concept script for myself to see how IPC::Shareable is actually working.

use Parallel::ForkManager; use IPC::Shareable; my $pm = new Parallel::ForkManager(10); my %options = ( create => 1, exclusive => 0, mode => 0644, destroy => 1, ); my $testvr = 0; tie $testvr, 'IPC::Shareable', 'data', \%options; for(my $i = 0; $i < 100; $i++) { $pm->start and next; (tied $testvr)->shlock; $testvr++; (tied $testvr)->shunlock; print $testvr . "\n"; $pm->finish; } $pm->wait_all_children;

This script works as intended, but when I try to modify $testvr variable inside for loop, it is not working correctly. What should I do to fix this issue? Any ideas?

Example:

for(my $i = 0; $i < 100; $i++) { $pm->start and next; if($testvr >= 5) { $testvr = 0; } (tied $testvr)->shlock; $testvr++; (tied $testvr)->shunlock; print $testvr . "\n"; $pm->finish; } $pm->wait_all_children;

The output should be:

1
2
3
4
5
1
2
3
4
5
...

but it is:

1
2
3
4
5
1
1
2
3
4
5
6

(locking variable inside if statement is not working)

I am sure I am missing something obvious but could not find solution.

Thanks in advance,

In reply to IPC::Shareable and Parallel::Forkmanager Question by anlamarama

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.