LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to know how manv times the dequeue element function is called?

Solved!
Go to solution

Hey, I'm developing a producer-consumer VI and I want to stop both loops when I dequeue 100 elements. How can I count how many elements have been dequeued or how many times the dequeue element function was called?

 

Could you help me please? Thankss

0 Kudos
Message 1 of 8
(931 Views)

You can use the iteration counter to count to 99 (value of 99 since it is zero-based).

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 2 of 8
(921 Views)

Unless you're enqueuing from multiple producers, I think it'd be smarter to count on the producer side and stop enqueuing when you get to 100.  That's more in the spirit of the producer / consumer pattern where the producer's job is to *defer* the needed work and the consumer's job is to complete it.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy coming to an end (finally!). Permanent license pricing remains WIP. Tread carefully.
Message 3 of 8
(906 Views)

I did this actually, but when I see my tdms file I don't have 50 elements, I have 0. So I assumed that the consumer loop is called without any elements on the queue or something like that.

 

euqinom2727_0-1718717988138.png

 

0 Kudos
Message 4 of 8
(900 Views)

If the timed out? output of the Dequeue Element function is true, you have no data, so there is nothing to save.

Also, you should not use a null timeout, as the loop would spin too fast even if there are no data.

Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
0 Kudos
Message 5 of 8
(864 Views)

It seems that the timed out? it's never true, so I always have data, so my theory is not true. I could count how many times I write to the file, but I don't know how to do it either

0 Kudos
Message 6 of 8
(837 Views)
Solution
Accepted by euqinom2727

In a Producer/Consumer design, the Producer "produces", and is "responsible" for controlling (and counting, if that's important) the things being "produced".  The Producer is also responsible for knowing when Production is done (since the Producer "gets the data first").

 

The Consumer has no idea when the data will be coming, but knows to process it as it comes, until it gets a sign from the Producer that "Production is done", in which case the Consumer can stop waiting for more data and close whatever it is doing with the data.

 

So, the Producer simply counts 1, 2, 3, ... until it gets to 100 (or, if you are doing this in a For Loop, the Producer has 100 written to the "N" of the For loop).  When it is done, it sends a "signal" (called a "sentinel") to the Consumer saying "I'm Done".

 

Until 2016, LabVIEW handled Producer/Consumer Designs with Queues.  You enqueue 100 "elements" in the Producer and it goes to the Consumer.  How do you signal you are done?  NI seems to do this by having the Producer releasing the Queue, which causes the Consumer to throw an Error.  Bad!  Better is for the Producer to Enqueue One More Time and send a Sentinel.  Since a frequent quantity being passed in P/C designs are Arrays of Data, a great Sentinel is an Empty Array.  The Producer exits the For Loop (bring the Queue with it), and enqueues an Empty Array.  The Consumer is modified so when it Dequeues, it checks for the Sentinel (Empty Array? function) and uses a Case Statement to Process Data if False (meaning Not Empty Array) or Releasing the Queue (safely!) and stopping the Consumer Loop if True.

 

In 2016, LabVIEW got Asynchronous Channel Wires.  The Stream Channel is what you use for Producer/Consumer.  No more "data wire going backwards".  It also has "Element Valid?" and "Last Element?" inputs in the Channel Writer (Producer), ideal for signaling the same to the Channel Reader (Consumer).  Best of all, you only need Writer and Reader, as Writer also does "Obtain Stream" and Reader will do "Release Stream" when it gets "Last Element" is true.

 

Bob Schor

Message 7 of 8
(794 Views)

Nice, I used the stream channel and it worked, thank u

0 Kudos
Message 8 of 8
(753 Views)