01-19-2026 03:42 AM
Hi everyone!
I'm using QMH template.
I would like to enqueue a sequence of tasks to be done while program is to be closed. For example: User stops the program -> instruments are shut down -> communication is terminated and closed -> queue is flushed and program stopped.
Of course solution shown bellow do not work, because there is a race condition. tasks will not be done in this order. Most of the time only 1st and 3rd tasks will be done.
I found this topic, but it's only idea: https://forums.ni.com/t5/LabVIEW-Idea-Exchange/Enqueue-array-of-elements/idi-p/970556
How should I enqueue those tasks correctly?
(of course the easiest way would be to just implement all those tasks in "exit" case of QMH, but since I've already made those cases for other parts of program, I want to reuse them in end sequence)
Best regards Dominik
Solved! Go to Solution.
01-19-2026 04:30 AM
Hi there,
The normaly way would be to enqueue the elements at the end of the queue (with enqueue element) not at the beginning (with enqueue element at opposite end)
Because what now happens is the following:
(To simplifiy it I call the elements just 1,2,3)
1. 1 is inserted into queue
2. if you are lucky 1 is taken from the queue before 2 is inserted (there we have already a race condition)
3. while 1 is processing 2 is inserted to the beginning
4. while 1 is still processing 3 is inserted in the queue, but as it is inserted to the beginning is before 2 in the waitlist
5. Processing of 1 is finished, so next element is 3.
6. 3 is processed.
7. When 3 is finished, the programm is ended (I guess)
So to solve it, include the elmenents at the end of the queue (with enqueue element)
If you need to priorize the end task, you can also insert a "End procedure" element at the beginning of the queue (with enqueue element at opposite end) like now. When the consumer structure processes this task you can flush the queue and insert the 3 elements above at the end of the queue (with enqueue element)
01-19-2026 05:14 AM
Hi there,
The normaly way would be to enqueue the elements at the end of the queue (with enqueue element) not at the beginning (with enqueue element at opposite end)
Because what now happens is the following:
(To simplifiy it I call the elements just 1,2,3)
1. 1 is inserted into queue
2. if you are lucky 1 is taken from the queue before 2 is inserted (there we have already a race condition)
3. while 1 is processing 2 is inserted to the beginning
4. while 1 is still processing 3 is inserted in the queue, but as it is inserted to the beginning is before 2 in the waitlist
5. Processing of 1 is finished, so next element is 3.
6. 3 is processed.
7. When 3 is finished, the programm is ended (I guess)
So to solve it, include the elmenents at the end of the queue (with enqueue element)
If you need to priorize the end task, you can also insert a "End procedure" element at the beginning of the queue (with enqueue element at opposite end) like now. When the consumer structure processes this task you can flush the queue and insert the 3 elements above at the end of the queue (with enqueue element)
01-19-2026 05:28 AM
Hi Dominik,
@Dominik.Bejma wrote:
Of course solution shown bellow do not work, because there is a race condition. tasks will not be done in this order. Most of the time only 1st and 3rd tasks will be done.
How should I enqueue those tasks correctly?
It would work correctly once you enqueue the commands in the correct order…
Use the "default" enqueue function instead of the "enqueue at opposite end"!
01-19-2026 05:42 AM
Yes, that's right. They will work that way, however, enqueueing commands in "correct order" will not guarantee immediate execution. First all the queue have to be processed.
If I think right, in Your solution queue must be flushed prior to enqueueing end sequence, like in pic bellow? Am I right?
01-19-2026 06:08 AM
Hi Dominik,
@Dominik.Bejma wrote:
however, enqueueing commands in "correct order" will not guarantee immediate execution. First all the queue have to be processed.
If I think right, in Your solution queue must be flushed prior to enqueueing end sequence, like in pic bellow? Am I right?
Yes, to execute commands from the queue all previous commands need to be dequeued/flushed from the queue.
The need to flush the queue depends on your specific implementation and requirements…
01-20-2026 03:53 AM
You add to the Front of the queue, so you'll Exit before the others are done. If you need to queue 3 commands to end, then do a Flush queue and queue up those finishing 3 as normal.