Question ID : 324
Created on 2021-12-02 at 1:51 AM
Author : Veryant Support [email@example.com]
Online URL : http://support.veryant.com/support/phpkb/question.php?ID=324
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-callDo 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 !
Back to Original Question