2.3   Threading Issues with Graphics

Various windowing systems have different restrictions on multithreading that have an impact on HALCON graphic operators.

2.3.1   Microsoft Windows

On Microsoft Windows, accessing a window's message queue is only possible from the thread that created the window. Furthermore, a window that has a parent window must be opened in the thread that created the parent window.

All HALCON graphic operators are automatically redirected to the correct thread. This is done by sending a special message to the window. To avoid conflicts with user code, the ID of this message is dynamically generated using the Win32 RegisterWindowMessage function. For windows not created by HALCON (this is relevant to open_ext_window and when specifying a parent window to open_window, the (non-HALCON) window is automatically subclassed using the Win32 SetWindowSubclass function. The only requirement for user code is that for this mechanism to work, each window must have an active message loop.

To avoid having to write user code to handle the message loop of a HALCON window, HALCON can be instructed to create all top-level HALCON windows from a special thread via set_system('use_window_thread','true'), which will then also take care of the message loop. Note that this may negatively affect performance if many windows are used simultaneously, as HALCON provides only a single thread for all windows.

2.3.2   X11

When using the X11 windowing system, HALCON will automatically call the Xlib function XInitThreads when the first window is opened. Note that HALCON will open a hidden window during initialization on systems with OpenGL support to determine which OpenGL features are supported.

This means that if an application using HALCON also uses Xlib functions independently of HALCON, either directly by explicitly calling Xlib functions or indirectly through some other library that uses Xlib, care must be taken that XInitLibrary is called first before any other Xlib function. The easiest way to ensure this is to call XInitThreads very early in the application's main function, before calling any other library functions.

2.3.3   macOS

On macOS, any operation that manipulates the NSView hierarchy must be done from the main thread. Furthermore, to ensure events are processed in the correct order, any access to the event queue should also be done from the main thread.

All HALCON graphic operators are automatically redirected to run from the main thread. This is done by using Grand Central Dispatch to dispatch the execution of HALCON operators as tasks to the main thread's dispatch queue. In order for this to work, the main thread must call dispatch_main or configure a run loop as described in Apple's “Concurrency Programming Guide” for macOS. Please note that since HALCON uses AppKit to display windows, the main thread should also be handling events from the window server. Standard Cocoa applications that call NSApplicationMain satisfy this requirement automatically, but C or C++ code exported by HDevelop does not, as this requires creating an application bundle, which is beyond the scope of the HDevelop code exporter.

2.3.4   Additional Considerations Applicable to Windows and macOS

The various HALCON draw_* operators work by actively polling the message (Windows) or event (macOS) queue. Since all graphics operators are run on the main thread in macOS, only a single draw_* operator can be active at any given time. On Windows, several draw_* operators can be active if they target windows belonging to different threads.