Skip to content

Threading issues with graphicsđź”—

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

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 for new_extern_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

This may negatively affect performance if many windows are used simultaneously, as HALCON provides only a single thread for all windows.

The various HALCON draw_* operators work by actively polling the message queue. Several draw_* operators can be active if they target windows belonging to different threads.

When using the X11 windowing system, HALCON will automatically call the Xlib function XInitThreads when the first window is opened.

Note

Querying the available OpenGL features will open a hidden X11 window in the background.

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, take care 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.