my @keycards= qw(A B C);
my @users = qw(peter james john luke andrew judas);
...
foreach my $user (@users) {
sleep(int(rand(3)));
my $key = pop(@keycards);
$pm->start and next;
as been said, it's a race condition.
- peter pops keycard and starts
- james pops keycard and starts
- john pops keycard and starts
- luke pops keycards. It starts if one (or more) of peter, james or john has already finished. Otherwise it waits for one of them to quit. But what kind of keycard does it have?
Fixing the problems with forks (or threads) by randomly inserting calls to
sleep is a TERRIBLE idea. Just don't ever do it!
It seems to me this problem could be solved by
my $pm = Parallel::ForkManager->new(2);
or, more generally
my $pm = Parallel::ForkManager->new(@keycards - 1);
All in all, that looks like a fairly questionable, error-prone feature of
Parallel::ForkManager (which is a good module otherwise).
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.