01-12-2009 05:02 PM - edited 01-12-2009 05:03 PM
hi,
i am trying to learn how the producer/consumer structure works and i am currently stuck understanding the queue fundamentals.
i am trying to create 2 loop. one is to control using events and one is the consuming loop.
i am trying to get the 2nd loop to stop when i click on "STOP" but i am not able to do so. can someone take a look at my example and help me out.
thank you.
01-12-2009 05:58 PM
Just an idea to work with. Since you are using an event structure you probably have a stop event. I would use the event structure to determine what state the consumer loop uses next. Since you can queue a cluster you could send the correct state to the queue, dequeue in the consumer loop, unbundle by name and set the state of the state machine. Just select the proper type def enum constant in each event of the event structure and queue it up.
Hope this makes sense and helps.
Happy coding.
01-13-2009 01:51 AM
The inner while loop is basically a no-no, but if you want it I would go for a notifier to transer the 'stop' state.
Ton
01-13-2009 05:03 AM - edited 01-13-2009 05:05 AM
You haven't enqueued anything so the Dequeue Element in your consumer loop is waiting for an event that never happens. The while loop that is supposed to stop when the Stop boolean is pressed doesn't even start. I've solved it by wiring a constant to your Dequeue Element, but you'll need to handle this however is appropriate for your application. Run your code with Highlite Execution turned on and you'll see what I mean.
01-13-2009 06:24 AM - edited 01-13-2009 06:24 AM
Your approach lacks of some important things. First of all, Producer Consumer State Machine.vi is a complete mess and will not work.
Producer Consumer State Machine demo.vi is the one you attached as screenshot as well; this one is better, but still not in a good shape.
So here my suggestion:
1.) Split Data from Commands. Your queue passes all data in your application. This is not good. In general, use commands to inform the consumer "that something happened". It is suggested to use a typified enum for this. If you have to pass data using the queue as well, transfer only the needed part as data in form of a variant datatype. Use the command to extract the correct datatyp from the variant in your consumer. See attached screenshot.
2.) Never use While Loops in the consumer. This is a guideline and may not fit all requisitions, but it keeps you from running into pitfalls.
3.) Define a special Shutdownprocedure. This can be e.g. sending "Stop" command from producer and then shutting down everything in your consumer. Please note that it might be useful to release the queue after leaving the consumer.\
4.) Do not use local variables to transfer data between producer and consumer. Get used to transfer data using the queue (screenshot), or functional globals (also known as action engine). Variables in general do no good to programming LabVIEW (with certain exceptions of course!).
Those are the most important suggestions which came into my mind right now.
hope this helps,
Norbert
01-13-2009 10:34 AM
i learn faster when looking at diagrams.
can you fix the Producer Consumer State Machine demo.vi and let me she what it can do to be fix?
thanks
01-13-2009 11:56 AM - edited 01-13-2009 11:58 AM
The Producer/Consumer Design Pattern (Events) (File_New_From Template_Frameworks_Design Patterns). The only thing I changed is that the template doesn't feed the dequeued element into the handler.
01-13-2009 12:41 PM
hmm, it seems like i am not getting it.
how would i stop the button loop without using locals?
i only want the bottom loop to end when i call for it, otherwise, it will go on forever.
01-13-2009 12:56 PM
The bottom loop will close because the top loop releases the queue reference. When this happens the Dequeue Element in the bottom loop will send an error into the condition terminal, causing it to stop. Turn Highlite Execution on and watch it happen.
Jim
01-13-2009 01:00 PM
i am watching the highlights.
the bottom loop closes but I have not yet click the stop bottom
it should only stop the bottom loop when the button is clicked on?