Spinlocks, the HALCON thread pool, and real-time scheduling🔗
By default, HALCON uses spinlocks for synchronization between threads to maximize performance. However, there are several situations when using spinlocks is not recommended:
-
If there are more threads running than there are CPUs to run them on, performance is severely reduced.
-
If any threads using HALCON use any form of real-time scheduling (
SCHED_FIFO,SCHED_RR, orSCHED_DEADLINEon Linux systems,REALTIME_PRIORITY_CLASSon Windows systems), HALCON can deadlock.
To turn spinlocks off, call the function HSetUseSpinLock(0) before calling the first HALCON operator.
Warning
Calling HSetUseSpinLock after HALCON has been initialized leads to undefined behavior.
Spinlocks and the HALCON thread pool🔗
The HALCON thread pool used for the automatic operator parallelization always uses spinlocks, even if HSetUseSpinLock has been called to turn them off.
Therefore, it is important to make sure the thread pool does not use more threads than CPUs are actually available to run them on.
Be especially careful if your program uses any form of real-time scheduling, as your program can deadlock otherwise.
HALCON will usually always create the thread pool during initialization.
If you do not need the thread pool, you can prevent it from being created by calling the function HSetStartUpThreadPool(0) before calling the first HALCON operator.
HALCON thread pool and CPU affinity🔗
On Windows systems, threads inherit the CPU affinity of the process by which they are created. This means that HALCON thread pool threads will be limited by the CPU affinity mask of the process at the time they are created.
HALCON only once at startup (during initialization) looks at the CPU affinity mask of the process to determine how many of the present CPUs can actually be used for automatic operator parallelization.
Therefore, if you change the CPU affinity subsequently, HALCON will not notice and may end up attempting to use more CPUs than are available to it.
On a machine with more than 64 processors, Windows will split the processors into multiple processor groups of at most 64 processors. For Windows versions prior to Version 11 or Windows Server 2022, processes are limited to a single processor group unless each thread is explicitly assigned a processor affinity. As HALCON does not modify CPU affinities, this means that HALCON is limited to using at most 64 processors for automatic operator parallelization.
Starting with Windows 11 and Windows Server 2022, processes may use all available processors by default, unless the process affinity mask has been set, in which case the process will revert to the pre-Windows 11 behavior.
HALCON can thus use more than 64 processors on Windows 11 and Windows Server 2022 if the process affinity mask is not modified.
If you want a program to limit which processors HALCON may run on but still use more than one processor group, do not set the process affinity mask, but instead set a default process CPU Set using the Windows CPU Set API prior to calling the first HALCON operator.
Note
While the Windows CPU Set API is partially available in Windows 10, HALCON will only take CPU sets into account on Windows 11 or Windows Server 2022 (or later), and only if no process affinity mask was set.
On Linux systems, threads inherit the CPU affinity of the parent thread that created them. This means that if the HALCON thread pool is enabled, its threads will be limited by the CPU affinity mask active of the thread creating the pool at the time the thread pool is created. If the pool is turned off, HALCON’s automatic operator parallelization will create threads on the fly. These threads will be limited to the CPU affinity mask of the thread calling the HALCON operator being parallelized.
HALCON only once at startup (during initialization) looks at the CPU affinity mask of the process to determine how many of the present CPUs can actually be used for automatic operator parallelization.
Therefore, if you change the CPU affinity subsequently, HALCON will not notice and may end up attempting to use more CPUs than are available to it.
This can degrade performance or lead to deadlocks when using any form of real-time scheduling.
Problems will also arise if the thread used to initialize HALCON uses a different CPU affinity mask than its parent process, as HALCON only takes the CPU affinity mask of the process into account.