Serializing file access with multithreading
To take full advantage of automatic serialization and to avoid explicitly writing your own serialization logic, use one of the recommended file organizations and usage patterns when you access files in threaded programs.
About this task
Use one of the following file organizations:
- Sequential organization
- Line-sequential organization
- Relative organization with sequential access
- Indexed organization with sequential access
Use the following pattern for input:
OPEN INPUT fn
. . .
READ fn INTO local-storage-item
. . .
* Process the record from the local-storage item
. . .
CLOSE fn
Use the following pattern for output:
OPEN OUTPUT fn
. . .
* Construct output record in local-storage item
. . .
WRITE rec FROM local-storage-item
. . .
CLOSE fn
With other usage patterns, you must take one of the following actions:
- Verify the safety of your application logic. Ensure that two instances of the program are never simultaneously active on different threads.
- Code explicit serialization logic by using calls to POSIX services.
To avoid serialization problems when you access a file from
multiple threads, define the data items that are associated with the
file (such as file-status data items and key arguments) in the LOCAL-STORAGE
SECTION
.
Example: usage patterns of file input and output with multithreading