LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

VI with more than one producer/consumer

Hi, everyone!

 

I am developing an event driven consumer producer state machine:

 

1. I have my loop with an event structure to handle me pressing the buttons. This is the producer loop which ensures states.

2. The consumer loop is then a message handler. It contains different cases and depending on the button pressed a state machine is enacted.

3. However, I have some indicators as well and I want to register events for them as well. I did it with the user event registration. Currently I have a separate loop for the generation of events. But I am scared that it might slow down the execution of my producer consumer. 

 

My question is: can I add an additional producer consumer just for my user events in the same VI? 

 

 

Best regards 

 

 

0 Kudos
Message 1 of 7
(2,020 Views)

There are several questions here, but it is certainly possible (and sometimes even advantageous) to have multiple Consumer/Producer loops running "cooperatively".

 

I don't usually think of an Event Loop as a Producer, though it can certainly function that way.  What is nice and "special" about the Event Loop is that it spends 99+% of its time "idle", doing nothing except waiting for any of the Events for which it is registered to fire.  When an Event does fire, the Event Loop does whatever it is programmed to do.  One of many things it could do is to "Produce" something going to a Consumer loop.  But if you have two different Consumer loops, each waiting for a different Event, there is no reason why a single Event Loop can't handle both of them.

 

Bob Schor

0 Kudos
Message 2 of 7
(2,011 Views)

Thank you for your answer!

I have a question regarding the same topic, but about the waits in the consumer loop. 

 

Currently my program has  one 1 event loop, then my message handler aka state machine and then a while loop which generates the user events for indicators. 

 

However, this made my code quite laggy. I realized it was because I put a wait function of 500 in my consumer loop. Now in my consumer loop I have a wait function of 60 and in the while loop for event generation I have a wait of 10. 

Now everything seems to work, however I wanted to ask if it is okay to have the wait function in my consumer loop? The problem is that if I do not put it there, i cannot stop my program while it is executing a state in the state machine, eg an elapsed time countdown. 

 

0 Kudos
Message 3 of 7
(1,997 Views)

One of the interesting things about Parallel Looping (whether Event Loops, State Machines, QMHs, Producer/Consumer, etc.) is planning how to stop them appropriately.  It is generally not a Good Idea to have a Global Stop routine.

 

I try to use TimeOuts to ensure that each loop "comes alive" periodically, typically every 100 msec.  If a TimeOut occurs, the TimeOut code can check to see if a Stop has been requested.  It is also my practice to have Producers exit first (as they usually get a signal to Stop), leave the Communication Path to the Consumer open (do not release the Queue here!), and send one last "Time to Exit" datum (called a Sentinel) to the Consumer, which then exits and closes the Communication Path.

 

If you are still having problems shutting down your system, compress the Folder holding the Project and attach the resulting .zip file for us to look at and suggest how you shut down safely.

 

Bob Schor

0 Kudos
Message 4 of 7
(1,990 Views)

Hello,

 

 

this would be a very simplified version of my code. It is what i described above.

 

However, the problem is that for some reason the loop with the generation of user events does not get executed. I am not sure if it because of the timing of the loops. I put the timing because otherwise I cannot stop my code.

Another problem is how to reinitialize the elapsed time vi if I would implement a restart button.

 

Thank you very much

 

Best regards

0 Kudos
Message 5 of 7
(1,895 Views)

I'm not sure what you are trying to do here, but most of your loops don't need (and should not have) "Wait" functions to slow them down.  For example, a Consumer loop should not have a Wait -- instead, it could have a TimeOut, but might not need it.  Similarly, an Event Loop is designed to wait for an "Event", so use an Event to stop it (i.e. put an "Value Change?" Event for the Stop control, so when you push Stop, among other things the Event loop "sees" this and can, if appropriate, wire "T" to the Stop terminal of the While loop surrounding the Event Structure.

 

Bob Schor

Message 6 of 7
(1,874 Views)

Hi, Bob! My real VI has a stop event in the event loop  which also sends the message Shutdown to the consumer loop. I think it should be present in my simplified VI as well.

About the timeouts:

 

I am not sure how to implement them in the timeout function since there are many of them and have different time targets. Also do they communicate with the events timeout function?

 

Best regards 

 

 

Best regards

 

 

0 Kudos
Message 7 of 7
(1,860 Views)