01-07-2011 07:48 AM
I built a producer/customer architecture type code and I ran into the problem that if the producer loops ends normally [because it produced the specified amount of data], it kills the queue as well so the consumer loop cannot output the data which is still in queue because the queue is destroyed - data is lost.
How is it possible to syncronise the two loops, how can I make the VI wait until the consumer empties the queue?
Thanks,
Krivan
01-07-2011 08:01 AM
Use a notifier or user event in the consumer to communicate back to the producer that it is done. You can use the queue status function to check how many items it contains.
01-07-2011 08:18 AM
01-07-2011 09:14 AM
Just out of curiosity:
as seen in the attached picture I moved the release queue VI from the producer loop to the consumer loop. This way I wanted to ensure that the queue is only released if the queue is empty. Now this implementation works according to the tests I have made so far and I don't use notifiers and the error handling is much easier.
Are there any drawbacks of my implementation? (Because in Felix's nugget various issues have been mentioned)
01-07-2011 09:29 AM
I don't see how you stop your program. Consumer loop stops on error, which never occures at the moment. How you stop it in the 'No error' case?
Felix
01-07-2011 09:39 AM
Yes, thanks for the comment! I also noticed it, for some reasons it wasn't obvious as I cared only about the data written into the files.
The attached file is the corrected version, it stops the VI.
01-07-2011 09:48 AM
What happens if you don't enqueue data in time? If on the first iteration of the consumer you have not yet put any data into the queue it will stop right away. The only reason it doesn't do that is because of the delay caused by the file dialog. I guess if it works then it works. But kind of a scary situation. You might change the code someday and move the TDMS initialize somewhere else and run into issues.
01-07-2011 10:24 AM
Is it possible to overcome this issue without using notifiers? As far as I know the use of notifiers results in a lossy data transfer and unfortunately I must store every data points.
01-07-2011 10:36 AM
Just check back to my nugget. Check after the producer if the queue is empty (so the consumer is done) before releasing the queue to terminate the consumer. I think the code is attched there so you just drop it on your BD.
Or send an additional 'command' inside the queue that tells the consumer that the producer is done. If this 'flag' is set, stop the consumer, release the queue.
Felix
01-07-2011 10:41 AM
I prefer to use distinct messages to stop or exit the consumer loop and perform all cleanup code (releasing queues or notifiers, closing references, etc.) in a separate VI that is called after both the producer and consumer loops have exited. This helps to eliminate the type of race conditions that you are running into.