Use the operator par_join to wait for the completion of a single thread or a group of threads.
As an example why this is necessary suppose we want to call a procedure that performs some magic calculation in the background and returns a count number as a result. In the subsequent program lines we want to use that number for further calculations.
par_start <ThreadID> : count_objects(num)
...
for i := 1 to num by 1 // BEWARE: num might be uninitialized
...
endfor
Simply relying on the subthread to be fast enough is most likely going to fail. Therefore, an explicit call to par_join is required beforehand.
par_start <ThreadID> : count_objects(num)
...
par_join(ThreadID)
for i := 1 to num by 1
...
endfor
Note that in HDevelop it is not strictly required to use par_join because the main thread will always outlive the subthreads. However, omitting it might lead to trouble if the program is going to be executed in HDevEngine (see Programmer's Guide) or exported to a programming language. Similarly, access to global variables might need some additional synchronization if the program is going to be exported.
Given the example from the previous section, waiting for the finishing of all the threads that were started in a loop is achieved using the following lines.
convert_vector_to_tuple(ThreadIDs, Threads) par_join(Threads)
Please note that the thread IDs had been collected in a vector variable. Thus, the conversion to a tuple is necessary for par_join to work properly.
The par_join operator blocks the further execution of the procedure it has been called from until all specified threads have finished. In the subsequent program lines, results from the corresponding threads can then be accessed reliably.