Public Service Announcement

TomBascom

Curmudgeon
If you find yourself coding anything along these lines:

Code:
x = etime.
do while etime < x + something:
  /* nothing... */
end.
or perhaps:

Code:
etime( yes ).
do while etime < something:
  /* nothing... */
end.
please back away from the keyboard keeping your hands in plain sight, do NOT save your changes and turn in your programmer credentials.

If you have the misfortune of finding such code that someone else wrote -- drop what you are doing and remove it immediately. Then hunt down the perpetrator sabatouer, confiscate his or her credentials, and initiate an in-depth code review of all changes that that person has ever committed.
 
Last edited:

KMoody

Member
Okay, I know I'll look foolish for asking, but why is this dangerous? These blocks of code seem designed to do something for a certain amount of time ("something"). It's not a straightforward solution, but I don't see what's wrong with it. It may be a dumb question, but hey, that's how you learn. ;)
 

TomBascom

Curmudgeon
Asking "why" is not foolish. "Foolish" would be to actually code such a thing after having been told that it is a bad idea.

Those blocks of code will consume an entire CPU for that length of time.

Imagine a server with hundreds of processes simultaneously executing such a block of code...

This is a generally inadvisable thing to be doing. It is egregiously bad when you are looping in this way for multiple seconds. (It is still very bad when doing it for just a few milliseconds but any purported justification for multiple full seconds is beyond my comprehension.)

The correct approach is to use the PAUSE statement.

If you are stuck on some ancient, obsolete and unsupported release that does not support sub-second PAUSE you can use a DLL call to sleep for fractions of a second. (In case you missed it PAUSE has supported sub-second intervals since 11.something.long.ago.)
 

KMoody

Member
Thanks for your help, Tom. Why would these blocks of code consume the entire CPU? Does invoking ETIME in a loop conditional force other processes to wait until the loop ends?
 

TomBascom

Curmudgeon
It will consume the CPU because it isn't waiting for anything. It is executing "computation" with no IO and will thus be highly prioritized.

It is not much different from:
Code:
do i = 1 to 10000000:
end.
It will run as hard as it can until the condition is met.

If code is doing something *useful*, like totaling all of the orders for all customers then something that runs as hard as it can isn't bad. But when it is simply waiting then you are wasting CPU cycles. Lots of them.

The 4GL interpreter isn't magic - it cannot tell that you really mean to be doing the equivalent of a PAUSE.
 

KMoody

Member
Oh, now I understand! In your examples, I assumed that "/* whatever... */" was a placeholder for something useful. Thanks!
 
Top