vishi has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks!!!
Ok, this might seem like a noob question but I'm confused on Perl's behaviour. I have code that checks 2 flags, whether they are "true" or not. Until both of them are set to "true", my script is required to sleep and wait for them. Here's my code:
print "Waiting for all workers to finish their tasks ..."; while (($worker1Finished ne "true") && ($worker2Finished ne "true")) { print "."; sleep(1); }
Well, as you punters might have guessed, I'm having problems with the way Perl "short circuits" my logical AND (&&) operator in the while condition.
According to perldoc.perlop, Binary "&&" performs a short-circuit logical AND operation. That is, if the left operand is false, the right operand is not even evaluated. Scalar or list context propagates down to the right operand if it is evaluated. So, in my code, if worker 1 has not yet finished, the script will wake up before it is supposed to...and this is resulting in hara-kiri!
The functionality of my code is such that either worker1 can finish first, or worker 2... there is no "predefined" order in which these flags are set to "true". How do I ensure that both flags are checked for "true" and only after this, the execution moves forward ???
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Short circuits in Logical AND (&&)
by davido (Cardinal) on Aug 02, 2011 at 06:06 UTC | |
by vishi (Beadle) on Aug 02, 2011 at 06:36 UTC | |
by davido (Cardinal) on Aug 02, 2011 at 06:51 UTC | |
by vishi (Beadle) on Aug 02, 2011 at 08:59 UTC | |
|
Re: Short circuits in Logical AND (&&)
by kcott (Archbishop) on Aug 02, 2011 at 06:47 UTC | |
by GrandFather (Saint) on Aug 04, 2011 at 02:04 UTC | |
by locked_user sundialsvc4 (Abbot) on Aug 02, 2011 at 11:17 UTC | |
by JavaFan (Canon) on Aug 02, 2011 at 14:57 UTC |