2.2   Parallel Programming Using HALCON

HALCON supports parallel programming by being thread-safe and reentrant, i.e., 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.

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

Furthermore, HALCON provides special operators to synchronize threads (see section “Multithreading Operators”).

2.2.1   A Closer Look at Reentrancy

In fact 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.

    Please note that you must take special care when multiple threads use the same data objects, e.g., 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, i.e., 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 quite generally, 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 unwelcome 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, i.e., while such an operator is executed, all other threads cannot call another HALCON operator.

  6. independent
    A group of operators is executed independently from other, even exclusive operators.

As mentioned already, the description of a HALCON operator in the Reference Manuals contains an entry called 'Execution Information', which specifies its behavior when using HALCON. This entry specifies the level of reentrancy as described above.

2.2.2   Program Design Issues for Parallelization

The following issues have to be considered for multithreaded programming in general:

When using HALCON, please keep the following issues in mind:

2.2.3   Multithreading Operators

In the operator section System→Multithreading, HALCON provides operators for creating and using synchronization objects like mutexes, events, condition variables, and barriers.

With them, you can synchronize threads in a platform-independent way. Note, however, that up to now no operators for creating the threads are provided.

2.2.4   Examples

HALCON currently provides the following examples for parallel programming (paths relative to %HALCONEXAMPLES%):

HALCON/C

HALCON/.NET

HALCON/C++