Goroutines in COBOL
A lightweight conceptual equivalent of goroutines using COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. GoroutinesExample.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-ITER PIC 9(2) VALUE 0.
01 WS-MSG PIC A(20).
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM F("DIRECT")
MOVE "GOROUTINE" TO WS-MSG
PERFORM CONCURRENT-PARA
MOVE "GOING" TO WS-MSG
PERFORM CONCURRENT-ANONYMOUS
CALL 'CBL_TO_YIELD_THREAD' USING BY VALUE 1000
DISPLAY "DONE"
STOP RUN.
F SECTION.
F-PARA.
DISPLAY "DIRECT: " WS-ITER
MOVE 0 TO WS-ITER
PERFORM VARYING WS-ITER FROM 1 BY 1 UNTIL WS-ITER > 3
DISPLAY "DIRECT: " WS-ITER
END-PERFORM.
CONCURRENT-PARA SECTION.
PERFORM CONCURRENT-F-PARA.
CONCURRENT-F-PARA.
MOVE 0 TO WS-ITER
PERFORM VARYING WS-ITER FROM 1 BY 1 UNTIL WS-ITER > 3
DISPLAY "GOROUTINE: " WS-ITER
END-PERFORM.
CONCURRENT-ANONYMOUS SECTION.
DISPLAY WS-MSG.Explanation:
In this COBOL example, we attempt to mimic the concept of lightweight asynchronous execution (similar to goroutines) within COBOL’s constraints, as COBOL doesn’t have built-in support for threading or goroutines. We use the following idiomatic constructs for COBOL:
Simple Function Call:
- We utilize a
PERFORMstatement to sequentially call a simple function.
PERFORM F("direct")- We utilize a
Mimicking a Goroutine with a Separate Section:
- We simulate concurrent execution by defining a separate section
CONCURRENT-PARAand invoking it usingPERFORM.
PERFORM CONCURRENT-PARA- We simulate concurrent execution by defining a separate section
Anonymous Function-like Execution:
- In COBOL, we demonstrate an anonymous function call equivalent by directly displaying a message within a separate section
CONCURRENT-ANONYMOUS.
PERFORM CONCURRENT-ANONYMOUS- In COBOL, we demonstrate an anonymous function call equivalent by directly displaying a message within a separate section
Synchronization with Sleep:
- We use a hypothetical call to a system-specific sleep function
CBL_TO_YIELD_THREADto pause execution, imitatingtime.Sleep.
CALL 'CBL_TO_YIELD_THREAD' USING BY VALUE 1000- We use a hypothetical call to a system-specific sleep function
Output Explanation:
- The output will show the synchronous print statements followed by the “concurrent” print statements executed in different sections, implying rudimentary concurrency.
This COBOL example section demonstrates how to adapt concepts originally illustrated in other programming paradigms using the syntax and idioms specific to COBOL.
Comments powered by Disqus