LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

LabVIEW - How to stop Producer Loop with a condition in Consumer loop?

Solved!
Go to solution

I have a file that does this:

 

1. Reads Text file

2. formats text file into a 3xN array (each line of text is stored in each row).

Now I want a queue to process each of those elements (i separated each column of the array).

 

My problem: how can I stop the producer/consumer loops when the end of text file is reached (i.e. no more elements to be read) My queue loops simply bundle/unbundle/index arrays.

 

 

0 Kudos
Message 1 of 6
(4,311 Views)

I've discussed this before in this Forum (so I almost "know it by heart").  Here are Bob's Rules for Producer/Consumer Control:

  1. The Producer is the only one who "knows" when there's nothing more to put into the Queue.
  2. The Consumer is the only one who "knows" when there is no more need for the Queue.
  3. Thus the Producer needs to send a "signal" to the Consumer saying "That's All, Folks!".
  4. After the Producer sends this signal, the Producer can exit, leaving the Queue intact!!!
  5. Ideally the Producer "embeds the signal in the Data".  This is easily done in the following cases:
    1. If you are sending an Array (data, for example), send an empty Array.
    2. If you are sending a String, send an empty String.
    3. This "special" code is sometimes called a "Sentinel".
    4. A convenient place to send the Sentinel is just after the Producer ends (and still can use the Queue "one last time").
  6. The Consumer (which isn't in such a hurry) examines each data item as it is dequeued, checking to see if it is the Sentinel.
    1. If it is, it doesn't do anything, but exits from the Consumer Loop.
    2. If it is not, then it is "real data" and the Consumer "consumes" it as it is supposed to do.
  7. When the Consumer exits, it knows that the Producer has sent it the "I'm all done" signal, and no longer needs the Queue (as it has exited), so the Consumer Releases the Queue.
  8. The Producer has exited, the Consumer has exited, and you are done!

Bob Schor 

Message 2 of 6
(4,299 Views)

My question is-- how does the producer know if the file it's getting the queue from in another subvi has reached the end or not?

0 Kudos
Message 3 of 6
(4,294 Views)

@sam18 wrote:

My question is-- how does the producer know if the file it's getting the queue from in another subvi has reached the end or not?


I don't understand the question.  The Producer puts data into the Queue -- when the Queue is Obtained, it is brought into the Producer and the Producer enqueues.  You didn't attach your VI, so I don't know from where or how the Producer is getting its data (this is one reason we always ask that you attach your VI or VIs, as we can't advise you on details of your code that we can't see).  I see you have an "End of Data?" indicator -- if we saw all your code, we could probably suggest a better way to inform the Producer that it was time to exit.

 

Please don't ignore the comments I took the time to write for you.  You are closing the Producer/Consumer in the wrong (meaning "unsafe") way -- it might work, but you run the risk of losing data or having your program "hang".

 

Bob Schor

0 Kudos
Message 4 of 6
(4,281 Views)

I can't attach my VI because it will be used for something at my work.

To explain what I mean by "end of data"

 

Say I enter:

     hi low 8

     low hi 9

     low low 10

into a text file.

The text file is then converted into an array and displayed on front panel. 

The producer should stop queueing elements after it detects the last row in the array which can either be the 5th row or the 9348th row (depends on input text file). 

 

**Also, I am not ignoring your comments, I appreciate the information.

 

0 Kudos
Message 5 of 6
(4,277 Views)
Solution
Accepted by topic author sam18

The way you tell the Producer that it is time to quit is the same basic technique you use with the Producer/Consumer communication.  Somewhere you have a routine "feeding" data to the Producer.  Consider this a "Producer-Producer", and your Producer Loop a "Consumer-Producer".  Your text-reading Loop reads a line, sees data, and sends it to the Producer Loop.  I don't know how data gets there -- are you passing a String, or are you passing data?  In either case, you need to pass data in such a way as to allow for a Sentinel.

 

Let's say you pass a String.  The Text-reading Loop sees "hi low 8", and sends "hi low 8".  The Producer parses this and adds it to the Queue.  Now the Text-reading Loop reaches the end of the file, and is ready to stop.  It sends the Producer an empty String.  When the Producer sees this (it tests for an Empty String first, see the Comparison Palette), it exits and sends its Sentinel to the Consumer Loop.

 

On the other hand, lets say you pass "hi low 8" as a Cluster of two booleans and an I32.  Change the Cluster to have three booleans and an I32 -- the third Boolean is "End of Data", Fault by default.  Again, the Producer simply tests End of Data first and exits if it is true.

 

As a general rule, it is easier to suggest relevant Solutions when we see the relevant Problem.

 

Bob Schor

0 Kudos
Message 6 of 6
(4,270 Views)