Many developers use a CPU-intensive DO loop to "delay" an interactive or batch application for an estimated period. This practice is, in a word, wrong! Here's an example routine. Note the "delay" code highlighted in gray. This technique eats up CPU cycles for no other reason than to wait for a given interval. (Please don't try this at home!)
C DOU NOT %ERROR()
C CUSTNO CHAIN(E) CUSTREC
C if %ERROR()
C if %status() = REC_LOCK
C callp sndstsmsg('Record locked. +
C Retry in 15 seconds')
C DO 15000
C eval x = x + 1
C enddo
C iter
C endif
C endif
C leave
C enddo
Why not just use the DLYJOB CL command? Well, using CL commands in general is becoming more and more taboo lately. I think this is due to two advances:
- RPG IV's EXTFILE keyword on the File specification eliminates the need for, in my estimate, some 80% of the OVRDBF commands.
- There isn't much you can do in CL that can't be done in RPG IV using APIs or a good class library such as RPG xTools, and OPNQRYF has been all but replaced by embedded SQL.
The sleep() function suspends a job for a specified number of seconds. If the job receives an end request, such as ENDSBS, ENDJOB, or PWRDWNSYS, the function returns immediately and indicates how many seconds remain in the sleep request.
The sleep API is really just a C runtime function that is callable from RPG IV. In RPG xTools, we've masked the sleep() subprocedure in the DLYJOB() RPG xTools procedure. Actually, xTools uses an MI instruction to provide even more control, but if you want the same behavior as the sleep() API, RPG xTools also includes the sleep() prototype.
To use SLEEP in your own RPG IV program, you need to do two things:
- Specify the binding directory QC2LE in the BNDDIR keyword on the Header specification in the RPG IV source code. Remember to specify QC2LE in all uppercase letters and enclose it in apostrophes. (See example below.)
- Include the prototype for the sleep() C runtime function. I've included it at the end of this article, and it is also available on the RPGIV.com downloads page.
There are two versions of sleep, with subtle but important differences:
- sleep accepts the number of seconds to delay the job.
- usleep accepts the number of microseconds to delay the job.
Here are the prototypes for the two versions of sleep.
D sleep PR 10U 0 ExtProc('sleep')
D seconds 10U 0 value
D usleep PR 10U 0 ExtProc('usleep')
D microSecs 10U 0 value
Using Sleep in RPG IV
The following illustrates using sleep() to wait 15 seconds before retrying a database I/O operation after a record lock is detected.
C DOU NOT %ERROR()
C CUSTNO CHAIN(E) CUSTREC
C if %ERROR()
C if %status() = REC_LOCK
C callp sndstsmsg('Record locked. +
C Retrying in 15 seconds')
C callp sleep(15)
C iter
C endif
C endif
C leave
C enddo
Do the right thing, and stop wasting CPU cycles.
Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.
LATEST COMMENTS
MC Press Online