I have a producer/consumer loop design in my application. The producer
enqueues data at the opposite end to achieve a LIFO stack like
characterstic. The problem is that the consumer loop processes data
faster than the producer consumer loop can process. I would like the
consumer to consume in the last entry that the producer places into the
stack. The problem right now is that the consumer starts consuming
before the producer is finished with everything, so there are still
data being placed onto the stack after the consumer loop starts going.
Does anyone have any ideas how to solve this problem.
What are you using for a stack? If you are using a queue, your consumer loop can check if the queue is empty. You could go into a loop where you wait until data is in the queue before processing it. Just check the queue for data in a loop. If no data, do nothing, if data, exit the loop and process. Put a delay in the loop, probably 10mS would do. Then your consumer would not be trying to process nothing.
Expanding a bit on tbob's suggestion: Set a timeout on the queue. Test the Timeout? output. If true, no data was received. If false process the data. The timeout becomes the delay/wait.
Also, can you process the the data in chunks rather than one at a time?