Multiple Execution Threads

ACUCOBOL-GT includes support for the specification and control of multiple execution paths in the program (multithreading). This means that you can define separately executing program threads and control their execution.

A thread is an execution path through a program. All programs have at least one thread, the thread (execution path) that starts when the program starts and ends when the program ends. In a program that has multiple threads, more than one thread may be active at a time.

When more than one thread is active, the runtime monitors and switches among threads, dividing processing time in accordance to program activity and thread priority (thread priorities are described below). In this way, threads can be thought of as running in parallel. Although conceptually they run in parallel, keep in mind that your program is still executing one statement at a time. The runtime controls the movement among active threads in response to a variety of conditions, such as computational activity, file I/O, user input, and programmatic controls, such as messages and locks (described below).

While most programs do not need multiple execution paths, there are some cases where threads can be helpful.

  1. Threads can be used to simplify modeless window handling. By tying threads to modeless windows you simplify the task of managing multiple windows. If each modeless window has its own thread, the runtime can automatically activate or suspend each window (via its associated thread). The code within each thread can then focus solely on managing its own window. This approach is much easier than the alternative, in which the program must be made to handle requests from any window at any time.
  2. Threads can improve system throughput. There are times when you can improve system throughput by performing tasks in one thread while waiting for user input in another thread. For example, you could do your program initialization while the user enters his or her password. Or, you could allow the user to edit the first item found by a complex search while your program continues searching for additional items.
  3. Threads can be used to control program actions. For example a program can present and accept a "cancel" dialog box in one thread while running a report in another. If the user clicks on the cancel button, that thread stops the report by terminating the report's thread.
  4. Threads can be used to perform periodic updates. Any periodic effect, such as updating an on-screen clock or performing animation, can easily be accomplished in a thread by placing that thread in a loop that sleeps for some period of time, wakes and performs the update, and then loops back to the sleep cycle. This allows the rest of your program to be written without concern about performing the periodic update.