To start a new thread, prefix the corresponding operator or procedure call with the par_start qualifier:
par_start <ThreadID> : gather_data() ...
This call starts the hypothetical procedure gather_data() as a new subthread in the background and continues to execute the subsequent program lines. The thread ID is returned in the variable ThreadID which must be specified in angle brackets. Unlike in HDevelop, in HDevEngine a given ThreadID is only valid within the procedure that started the thread.
Note that par_start is not an actual operator but merely a qualifier that modifies the calling behavior. Therefore, it is not possible to select par_start in the operator window.
If starting a new subthread would exceed the configured maximum number of threads (see above), an exception is thrown.
You can also start procedure or operator calls as a subthread from the operator window (see figure 8.3). To do this, open the section Advanced Parallelization Options at the bottom of the operator window, tick the check box and enter the name of the variable that will hold the thread ID. If you double-click on a program containing the par_start qualifier, the parallelization options will also be displayed in the operator window. For certain program lines (e.g., comments, declarations, loops, or assignments) par_start is not supported and the corresponding options will also not be available in the operator window. For a general description of the operator window see section “Operator Window” on page 357.
It is supported to start multiple threads in a loop. In that case the thread IDs need to be collected so that all threads can be referenced later:
ThreadIDs := []
for Index := 1 to 5 by 1
par_start <ThreadID> : gather_data()
ThreadIDs := [ThreadIDs, ThreadID]
endfor
It is often more convenient to collect the thread IDs in a vector variable:
for Index := 1 to 5 by 1
par_start <ThreadIDs.at(Index - 1)> : gather_data()
endfor
Special care must be taken when the subthread returns data in an output variable. In particular, output variables must not be accessed in other threads while the subthread is still running. Otherwise the data is not guaranteed to be valid.
Likewise, its must be ensured that multiple threads do not interfere with their results. Suppose the procedure gather_data is started as multiple threads like above but returns data in an output control variable:
for Index := 1 to 5 by 1
par_start <ThreadIDs.at(Index - 1)> : gather_data(Result) // BEWARE!!!
endfor
In the above example, all the threads would return their result in the same variable which is certainly not what was intended. The final value of Result would be the (unpredictable) return value of the thread that finishes last, and all other results would be lost.
An easy solution to this problem is to collect the returned data in a vector variable as shown previously with the thread IDs:
for Index := 1 to 5 by 1
par_start <ThreadIDs.at(Index - 1)> : gather_data(Result.at(Index - 1))
endfor
Here, each invocation of gather_data returns its result in a unique slot of the vector variable Result.