When doing multithread programming, it is a best practice that a program should not finish its processing until all of its threads are finished as well.
In this article we provide a sample logic of how to check that all the threads started by a program are finished.
The first step is that when you call a thread, you add the thread handle of that call to a dynamic array similar to the following:
call thread "progA"
handle in W-Th
*> using... would go here
on overflow
display "main: progA not found"
not on overflow
add 1 to max-progs
move W-Th to Prog-H-Thread(max-progs)
move "progA" to Prog-Name(max-progs)
end-call
Do the same for any thread started by the main program.
WAIT-FOR-THREADS.
perform until 1 = 2
move 0 to ws-threads-running
perform varying idx-prog from 1> by 1
until idx-prog > max-progs
if Prog-H-Thread(idx-prog) not = null
wait for Prog-H-Thread(idx-prog)
test only
status in StatusWait
if StatusWait = "10" |*> The thread does not exist anymore, it's not running
move spaces to Prog-Name(idx-prog)
move 0 to Prog-H-Thread(idx-prog)
else
add 1 to ws-threads-running |*> Thread's still running, count it as running
end-if
end-if
end-perform
if ws-threads-running = 0
exit perform
end-if
end-perform.
Please download the Zip file with a full sample of this.
> iscc *.cbl
> iscrun MAIN
main: Waiting for threads to finish ...
progB: finished
main: thread finished, program: progB
progC: finished
main: thread finished, program: progC
progA: finished
main: thread finished, program: progA
main: All threads are done, program finished !
Article ID: 324
Created: December 2, 2021
Last Updated: December 2, 2021
Author: Support KB Author
Online URL: https://support.veryant.com/phpkb/article.php?id=324