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”).
In fact there are different “levels” of reentrancy for HALCON
operators:
- 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.
- local
Operators marked as local should be called only from
the thread that instantiates the corresponding objects.
- 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.
- mutually exclusive
Some operators cannot be called simultaneously by multiple threads but may be
executed in parallel to other HALCON operators.
- 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.
- 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.
The following issues have to be considered 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
might actually be slower than before because of the synchronization
overhead. Note that when counting threads only the so-called
worker threads are relevant, i.e., threads that are running /
working continuously.
- Local variables
If possible, use local variables, i.e., 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; please refer to the
documentation of your programming language for details).
When using HALCON, please keep the following issues 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 which create or delete files work exclusively,
i.e., other threads have to wait.
The programmer has to assure that threads do not access the same file (or
handle) simultaneously!
See section “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 section “Customizing the Parallelization Mechanisms”.
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.
HALCON currently provides the following examples for parallel programming
(paths relative to %HALCONEXAMPLES%):
HALCON/C
- c\source\example_multithreaded1.c
two threads extract different elements on a board in parallel
HALCON/.NET
- c#\MultiThreading (C#)
performs image acquisition, processing, and display in three threads
- hdevengine\c#\MultiThreading (C#)
executes the same HDevelop procedure in parallel by two threads using HDevEngine
- hdevengine\c#\MultiThreadingTwoWindows (C#)
executes different HDevelop procedures in parallel by two threads using HDevEngine
HALCON/C++
- mfc\FGMultiThreading (using MFC)
performs image acquisition / display and processing in two threads
- mfc\MultiThreading (using MFC)
performs image acquisition, processing, and display in three threads
- hdevengine\mfc\source\exec_programs_mt_mfc.cpp
executes HDevelop procedures for image acquisition, data code reading, and
visualization in parallel using HDevEngine and MFC
- hdevengine\cpp\source\exec_procedures_mt.cpp
executes HDevelop programs in parallel using HDevEngine