8.10.3   Execution of Threads in HDevelop

In general, threads in HDevelop are only executed in parallel when the program runs continuously after pressing F5. In all other execution modes only the selected thread is started and all other threads remain stopped unless an explicit user interaction advances their execution. Active breakpoints, stop instructions, runtime errors or uncaught exceptions also cause all threads to stop so their current state can be evaluated. This convention enables a clearly defined debugging process because it eliminates uncontrollable side-effects from other threads. Any editing action in the program window will also cause a concurrently running program to stop.

Threads cannot be “killed” externally. They can be stopped between operator calls or by aborting interruptible operators. If any thread executes a long-running operator that cannot be interrupted at the time HDevelop tries to stop the program execution, a corresponding message will be displayed in the status line, and the corresponding thread will eventually stop after the operator has finished.

Selected Thread

Exactly one of the threads is the so-called selected thread; by default this corresponds to the main thread of the program. The position of the PC, the status of the call stack, and the state of the variables in the variable window are linked to the selected thread. The selected thread can change automatically to a thread that stops, e.g., by a breakpoint, stop instruction, an uncaught exception, or a draw operator.

How to select a specific thread is described in section “Inspecting Threads” on page 892. All run modes other then continuous execution apply only to the selected thread. Program lines unrelated to the selected thread will be grayed out in the program window.

Threads and Just-in-time Compiled Procedures

Procedures can be executed as compiled bytecode instead of being interpreted by the HDevelop interpreter. This is described in section “Just-In-Time Compilation” on page 96. There is one notable difference when debugging threaded HDevelop programs with compiled procedures. If the program is running continuously and is then being stopped (either by user action or a breakpoint/stop instruction), the current state of the compiled procedures (variables, PC) cannot be inspected. You can still step into the procedure calls but this will cause the corresponding thread to be re-executed which may cause unexpected side-effects. Note that this is not an issue when single-stepping into the thread calls in the first place because in that case procedures are always executed by the HDevelop interpreter.

Thread Lifetime

A thread exists as long as it is still being referenced by a variable even if its execution has finished. This is necessary to reference the thread in a par_join instruction, or to set back the PC of the corresponding thread for debugging. However, the lifetime of a thread ends if the PC is manually set back to a program line before its invocation. Apart from that the lifetime of all subthreads ends when the program is reset using F2. During its lifetime a thread is listed in the Thread View / Call Stack window from where it can be selected and managed.

Finished, but still “living” threads might cause the configured thread number limit to be exceeded unintentionally when new threads are started at a later time. Also, it can have a negative effect on the run time behavior if finished threads are “killed” at the same time new threads are being executed. To explicitly “kill” finished threads, it is sufficient to reset the variables that are referencing their thread IDs as in the following example.

  for Index := 0 to 4 by 1
    par_start <ThreadIDs.at(Index)> : gather_data()
  endfor
  ...
  convert_vector_to_tuple (ThreadIDs, Threads)
  par_join (Threads)
  ...
  ThreadIDs := {}
  Threads   := []

Error Handling

Each thread can specify its own error handling, e.g., using dev_set_check('~give_error'). New subthreads inherit the error handling mode from their parent thread. Exception handling using try .. catch works only within a thread, i.e., in the main thread it is not possible to catch an exception that is thrown in a subthread.