Preparing COBOL programs for multithreading
You can run COBOL programs in multiple threads within a process under batch, TSO, IMS, or z/OS® UNIX.
There
is no explicit COBOL language to use for multithreaded execution;
rather, you compile with the THREAD
compiler option.
COBOL
does not directly support managing program threads. However, you can
run COBOL programs that you compile with the THREAD
compiler
option in multithreaded application servers, in applications that
use a C/C++ driver program to create the threads, in programs that
interoperate with Java™ and use Java threads, and in applications
that use PL/I tasking. In other words, other programs can call COBOL
programs in such a way that the COBOL programs run in multiple threads
within a process or as multiple program invocation instances within
a thread. Your threaded application must run within a single Language Environment® enclave.
Choosing LOCAL-STORAGE
or WORKING-STORAGE
: Because
you must code your multithreaded programs as recursive, the persistence
of data is that of any recursive program:
- Data
items in the
LOCAL-STORAGE SECTION
are automatically allocated for each instance of a program invocation. When a program runs in multiple threads simultaneously, each invocation has a separate copy ofLOCAL-STORAGE
data. - Data items in
the
WORKING-STORAGE SECTION
are allocated once for each program and are thus available in their last-used state to all invocations of the program.
For
the data that you want to isolate to
an individual program invocation instance, define the data in the LOCAL-STORAGE
SECTION
. In general, this choice is appropriate for working
data in threaded programs. If you define data
in WORKING-STORAGE
and your program changes the contents
of the data, you must take one of the following actions:
- Structure your application so that you do not access
data in
WORKING-STORAGE
simultaneously from multiple threads. - If you do access data simultaneously from separate threads, write appropriate serialization code.