Example: usage patterns of file input and output with multithreading
The following examples show the need for explicit serialization logic when you deviate from the recommended usage pattern for file input and output in your multithreaded applications. These examples also explain the unexpected behavior that might result if you fail to handle serialization properly.
In each example, two instances of a program that contains the sample operations are running within one run unit on two different threads.
READ F1
. . .
REWRITE R1
In the example above, the second thread might execute
the READ
statement after the READ
statement
is executed on the first thread but before the REWRITE
statement
is executed on the first thread. The REWRITE
statement
might not update the record that you intended. To ensure the results
that you want, write explicit serialization logic.
READ F1
. . .
* Process the data in the FD record description entry for F1
. . .
In the example above, the second thread might execute
the READ
statement while the first thread is still
processing a record in the FD
record description
entry. The second READ
statement would overlay the
record that the first thread is processing. To avoid this problem,
use the recommended technique:
READ F1 INTO LOCAL-STORAGE-item
Other
cases: You must give similar consideration to other usage patterns
that involve a sequence of related input and output operations, such
as START
followed by READ NEXT
,
or READ
followed by DELETE
. Take
appropriate steps to ensure the correct processing of file input and
output.