LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Producer/Consumer design pattern and timed loop

Dear All!
The built-in template for the Producer/Consumer design pattern is based on while loop structure.
 
Is it better to use timed loop instead of while loop?
 
There is an example Modes for the timedloop.vi that offers Producer/Consumer feature built around  timed loop.
 
With best regards,
 
Mikhail Kozlov
0 Kudos
Message 1 of 10
(3,817 Views)

hi there

i think it depends on your application: if you're forced to maintain exact timing and priority i'd use a timed loop for the producer loop. if not, i'd use a standard loop with "wait ms" or "wait until ms multiple" function.

cause the consumer loop waits fot the event that there is some data in the queue until "time out ms" i'd use a standard loop for the consumer without "wait ms" or "wait for ms multiple". if you'd use a timed loop for the consumer you would waste the rest of the time of one single turn doing nothing.

 

Best regards
chris

CL(A)Dly bending G-Force with LabVIEW

famous last words: "oh my god, it is full of stars!"
0 Kudos
Message 2 of 10
(3,808 Views)

Dear Chris,

thank you for your assistance very much.

I am going to use Producer/Consumer design pattern for design user interface (Producer) and test sequence (Consumer).

After your response my question can be rebuilt like

What is better to use queue with until "time out ms" (by the way in Producer/Consumer design pattern time out value is -1, no time out) or to build Consumer around timed loop and queue without until "time out ms"?

Another words, if I will design several Consumers than queue with until "time out ms" will stop execution in other Consumers or not?

With best regards,

Mikhail

0 Kudos
Message 3 of 10
(3,804 Views)
i see..
 
use a standard loop for each consumer and supply an approbiate value for "time out ms", e.g. 1000 ms.
 
But be careful: use "Dequeue Element" to remove an element from the queue and "Preview Queue Element" if you don't want to remove the element (remember: if you remove an element from the queue the other consumers won't see this element). or think about using a Notifier instead of a queue.
 
Best regards
chris

CL(A)Dly bending G-Force with LabVIEW

famous last words: "oh my god, it is full of stars!"
0 Kudos
Message 4 of 10
(3,797 Views)

Dear Chris,

thank you for your assistance.

I am going to use unique queue for each consumer. I have got several rather slow devices (magnetic field facility, temperature cryostat, etc). It is the reason why I would like to use several consumer to be able to control (adjust) several devices simultaneously.

With best regards,

Mikhail Kozlov  

0 Kudos
Message 5 of 10
(3,793 Views)

take a look at the attachment.

you can use one single consumer loop with one single queue when you supply the datas origin with the queue data.

advantage: minimized code, minimized changes in code when to add a new data origin
disadvantage: data is processed only serial

 

Best regards
chris

CL(A)Dly bending G-Force with LabVIEW

famous last words: "oh my god, it is full of stars!"
0 Kudos
Message 6 of 10
(3,789 Views)

Dear Chris,

thank you for your assistance.

In my case I have a need in a code that allows me to run several consumers at the same time. For example one consumer control sweeping of magnetic field value defined by Producer (user), another control sweeping of temperature value defined by Producer (user). It is the reason why I would like to be sure that wait for timeout does not stop another consumer loop.

 

With best regards,

Mikhail Kozlov

0 Kudos
Message 7 of 10
(3,785 Views)

I have discovered that Producer/Consumer design pattern (as it is suggested in LabView 😎 generated error message if one stop program since Consumer loop aborted by error.

Is it possible to abort Consumer loop without rising error?

With best regards,

Mikhail Kozlov

0 Kudos
Message 8 of 10
(3,768 Views)

hi there,

you can wait for new queue elements in one loop without having to wait for "time out ms" to ellapse in another parallel loop.

to abort waiting simply release the queue. see online help for "Deque Element" - function:

"If queue becomes invalid (for example, the queue reference is released), the function stops waiting and returns error code 1122"

Best regards
chris

CL(A)Dly bending G-Force with LabVIEW

famous last words: "oh my god, it is full of stars!"
0 Kudos
Message 9 of 10
(3,759 Views)
Dear Chris,
thank you for your assistance very much.
 

As you mentioned

"If queue becomes invalid (for example, the queue reference is released), the function stops waiting and returns error code 1122"

it means that I have received an error message after stoping Producer/Consumer if I use error handler VI. In general it is possible to remove this error message however I am looking for a better solution. I will try to send by queue a stop command to  Consumer loop.


0 Kudos
Message 10 of 10
(3,756 Views)