Title here
Summary here
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:
PERFORM
statement to sequentially call a simple function.PERFORM F("direct")
Mimicking a Goroutine with a Separate Section:
CONCURRENT-PARA
and invoking it using PERFORM
.PERFORM CONCURRENT-PARA
Anonymous Function-like Execution:
CONCURRENT-ANONYMOUS
.PERFORM CONCURRENT-ANONYMOUS
Synchronization with Sleep:
CBL_TO_YIELD_THREAD
to pause execution, imitating time.Sleep
.CALL 'CBL_TO_YIELD_THREAD' USING BY VALUE 1000
Output Explanation:
This COBOL example section demonstrates how to adapt concepts originally illustrated in other programming paradigms using the syntax and idioms specific to COBOL.