Multithreading: Reentrant, quasi-reentrant, and threadsafe programs

Multithreading is a technique where a single copy of an application program can be processed by several transactions concurrently. For example, one transaction can begin to execute an application program. When an EXEC CICS command is reached, causing a CICS® WAIT and call to the dispatcher, another transaction can then execute the same copy of the application program.

Compare this technique with single-threading, which is the execution of a program to completion: processing of the program by one transaction is completed before another transaction can use it.

Multithreading requires that all CICS application programs are quasi-reentrant; that is, they must be serially reusable between entry and exit points. CICS application programs that use the EXEC CICS interface obey this rule automatically. For COBOL, C, and C++ programs, reentrancy is ensured by obtaining a fresh copy of working storage each time the program is invoked. You should always use the RENT option on the compile or pre-link utility, even for C and C++ programs that do not have writable statics and that are naturally reentrant. Temporary variables and DFHEIPTR fields inserted by the CICS translator are usually defined as writable static variables and require the RENT option. For these programs to stay reentrant, variable data should not appear as static storage in PL/I, or as a DC in the program CSECT in assembler language.

As well as requiring that your application programs are compiled and link-edited as reentrant, CICS also identifies programs as being either quasi-reentrant or threadsafe; these attributes are set on the PROGRAM resource definition. The following table shows you the CONCURRENCY and API settings that are available on the program, and the type of TCB that the application program is run on.

Table 1. Combination of PROGRAM CONCURRENCY and API settings and the type of TCB used.
CONCURRENCY attribute API attribute CICS TCB
CONCURRENCY(QUASIRENT) API(CICSAPI) The application program always runs under the CICS quasi-reentrant (QR TCB). If a switch to an open TCB occurs for a resource manager, CICS always returns to QR TCB before returning to the application.
CONCURRENCY(QUASIRENT) API(OPENAPI) Invalid combination. OPENAPI programs cannot run on a QR TCB.
CONCURRENCY(THREADSAFE) API(CICSAPI) The application program can run on a QR TCB or an open TCB. If a switch to an open TCB occurs for a resource manager, CICS stays on the open TCB when returning to the application. If a switch to the QR TCB occurs, CICS stays on QR TCB when returning to the application.
CONCURRENCY(THREADSAFE) API(OPENAPI) Same as CONCURRENCY(REQUIRED) API(OPENAPI).
CONCURRENCY(REQUIRED) API(CICSAPI) The application program always runs on an open TCB. CICS services do not require TCB key matching so the application always runs on a L8 open TCB. An OPENAPI resource manager uses an L8 TCB so no TCB switch is required if it is invoked. If a switch to the QR TCB occurs, CICS returns to the open TCB when returning to the application.
CONCURRENCY(REQUIRED) API(OPENAPI) The application program always runs on an open TCB. The key of the TCB must match the execution key of the program. CICS uses an L9 TCB if EXECKEY(USER) is set and an L8 TCB if EXECKEY( CICS ) is set. If the application is user key and an OPENAPI resource manager is invoked then a switch occurs from the L9 TCB to the L8 TCB. CICS returns to L9 TCB before returning to the application. If a switch to the QR TCB occurs, CICS returns to the open TCB when returning to the application.