I am having problems with using two producer and one consumer code. I am reading in digital data and also analog measurements in one producer loop. In another producer I am using a counter to take pulse width measurements. In the consumer loop I am displaying the analog,digital, and counter data. The problem I am having is only the Analog and Digital data are displayed but not the counter data( pulsewidth measurements). This setup requires that I monitor a digital input and utilize Change detection both rising and falling edges. On Detection of the edges I do an Analog acquisition. This is done in one Producer loop.
My other producer loop uses a counter to take pulse width measurements. The data in this loop is obtained and is accurate. It just never shows up in my consumer loop.
If I disable analog measurements it does show up. Racing condition maybe exists? If so what can be done to get this to work right. I have uploaded the project here. Thank you, help is greatly appreciated.
Solved! Go to Solution.
You should not be updating all indicators with every dequeue element. If data is sent from the top loop, then cnt0 will be the default value (0). If data is sent from the bottom loop, all other values besides cnt0 will be default. Usually there would be an enum or string which tells the "consumer" which case to run. Then inside each case you can do different things with the data.
My guess is that the top loop is running much faster than the middle loop, so any data with cnt0 gets drowned out.
That appears what I see to be happening also. The top loop is running faster. Thanks I'll look at putting in a case statement to handle the data differently as is arrives.
Why can't we have another consumer loop and a second queue?
You could, but the consumer loop does so little here, it makes more sense to get rid of it and just update the indicators right where the data is generated.
It is still a good exercise to code up the functionality where the queue also includes an instruction or message to the dequeueing loop as part of the data, because this is the way queued message handlers work.