Skip to content

Parallel programming using HALCONđź”—

HALCON supports parallel programming by being thread-safe and reentrant, that is, different threads can call HALCON operators simultaneously without having to wait. However, not all operators are fully reentrant. This section takes a closer look at the reentrancy of HALCON. Furthermore, it points out issues that should be kept in mind when writing parallel programs that use HALCON.

More Info

The example program example_multithreaded1.c in the directory examplec shows how to use multithreading to extract different types of components on a board in parallel using HALCON/C.

About reentrancyđź”—

There are different “levels” of reentrancy for HALCON operators:

  1. Reentrant
    An operator is fully reentrant if it can be called by multiple threads simultaneously, independent of the data it is called with.
    Take special care when multiple threads use the same data objects, for example, the same image variable. In this case, you must synchronize the access to this variable manually using the corresponding parallel programming mechanisms (mutexes, semaphores). Better still is to avoid such cases as far as possible, that is, to use local variables. Note that this is no special problem of HALCON but of parallel programming in general.

  2. Local
    Operators marked as local should be called only from the thread that instantiates the corresponding objects.

  3. Single write, multiple read
    A certain group of operators should be called simultaneously only if the different calling threads work on different data.
    As this thread behavior is not recommended in general, HALCON does not actively prevent it and thus saves overhead.This means that if you (accidentally) call such operators simultaneously with the same data, no thread will block but you might get undesirable effects.

  4. Mutually exclusive
    Some operators cannot be called simultaneously by multiple threads but may be executed in parallel to other HALCON operators.

  5. Exclusive
    A group of operators is executed exclusively by HALCON, that is, while such an operator is executed, all other threads cannot call another HALCON operator.

  6. Independent
    A group of operators is executed independently from others, even exclusive operators.

The description of a HALCON operator in the operator reference contains an entry called “Execution Information”, which specifies the level of reentrancy as described above.

Program design issues for parallelizationđź”—

Consider the following issues for multithreaded programming in general:

Number of threads ≤ number of processors or cores
If you use more threads than there are processors or cores, your application may actually be slower than before because of the synchronization overhead. When counting threads, only the so-called worker threads are relevant, that is, threads that are running or working continuously.
Local variables
If possible, use local variables, that is, instantiate variables in the thread that uses them.
If multiple threads use the same variable, you must synchronize their access to the variable using the appropriate parallel programming constructs (mutexes, semaphores; see the documentation of your programming language for details).

When using HALCON, keep the following in mind:

Initialization
HALCON is initialized implicitly at the first operator call. During this initialization, among others, the license is checked, HALCON memory management is set up, the thread pool for internal operator parallelization is started, internal system parameters and synchronization objects are initialized.
This initialization process is thread-safe in a multithreading environment.
Note that the default value of some system parameters can be changed before this initialization process. Compare the include/HGlobal.h file and some references to the corresponding parameters in this document. If used, make sure that all default values are set before calling the first operator in your multithreaded environment.
I/O and visualization
Keep in mind that operators that create or delete files work exclusively, that is, other threads have to wait.
The programmer has to ensure that threads do not access the same file (or handle) simultaneously!
See Threading issues with graphics for information about visualization issues on different operating systems.
Multithreading vs. automatic parallelization
If you explicitly balance the load on multiple processors or cores in a multithreaded program, we recommend that you
  • either switch off the automatic parallelization mechanism to get an optimal performance
  • or reduce the number of threads used by it so that the sum of threads does not exceed the number of processors or cores.
How to switch of the automatic parallelization or reduce the number of internal threads is described in Customizing the parallelization mechanisms.

Multithreading operatorsđź”—

The section System > Multithreading in the HALCON operator reference provides operators for creating and using synchronization objects like mutexes, events, condition variables, and barriers.

Using them, you can synchronize threads in a platform-independent way. However, up to now no operators for creating the threads are provided.