Halcon 12: Easy communication between threads via message queues

One of HALCON 12’s major new features is to parallelise procedures within HDevelop. When using multithreading in applications, an important matter is how to exchange data between different threads.

HALCON addresses this issue by using message queues. Message queues in HALCON are easy to use and they take care about synchronisation, so no external locking of resources is required.

The usual workflow is demonstrated below:

threads1

In this scenario, the producer thread acquires images from a camera and places messages in the queue. Such a message can be dequeued by a consumer thread. A consumer thread may perform specific image processing tasks like OCR, barcode reading, or measurements based on the variables in the message. Its results can also be enqueued in another queue for further processing or used directly.

Let us take a closer look at a message. A message may contain control and/or iconic variables. These variables can be accessed by a user-defined key – which is a simple string:

threads2

Only a few operator calls are necessary for setting up such a multi-threading communication. First of all, we need to create a queue, usually in the main procedure:

create_message_queue

A producer thread then creates a message:

create_message

A message can be imagined as a package or container, where the variables are stored. This is done via the operators:

set_message_tuple

set_message_obj

providing the variable itself and the user-defined key. An arbitrary number of iconic and control variables may be present in one message. Pay attention to handles, they will be treated as integer values, therefore storing any handle in the message will copy the handle value, but not the resource behind the handle.

If all the necessary values have been set, the message can be enqueued via

enqueue_message

 

Please note that a deep copy of your message will be placed in the queue. If you do not need the message any more, it should be cleared by calling:

clear_message

This has no influence on the variables of the message since they are also a deep copy of the original values.

A potential consumer thread may retrieve values with:

dequeue_message

The “unwrapping” of values is similar to their “packing”. By using:

get_message_tuple

get_message_obj

in combination with the keys, you will receive the desired values. Once all values have been retrieved, the message can be cleared again.

And in case the queue is not needed any more, it should be cleared as well

clear_message_queue

Please note that you can use a multitude of get_* and set_* operators to configure the message queues the way you want them to be. You may want to have a look at the documentation of the operators:

set_message_queue_param

set_message_param

get_message_param

get_message_queue_param

set_message_queue_param