Hello all,
If I have keyed data and the following pseudocode
waitfor_new_data(); samples = datareader.read() for(s in samples) { do_stuff(s); //do_stuff runs a long time, several min. }
If now one of the samples is updated by a new DDS message, will then the loop incoperate this update?
Or do I have to call read again? If so how is it possible to only read one sample at a time to get always the newest updates?
Thank you and best regards,
Andreas
Hello Andreas,
The
samples = datareader.read()
operation takes a snapshot of the samples available in the DataReader cache at the time the operation is called. So this list will not be affected my the reception of new samples. This is the expected, specified behavior. So given that thedo_stuff()
can take a while you should calldatareader.read()
after you are done to capture samples that have arrived while you were processing the previous batch. You could do this by wrapping everything inside a for loop as in:You can wrap the whole thing into another loop to read more sample after you have finished processing the sequence as in:
You can also read one sample at a time. This is what
DataReader::read(T&)
andDataReader::read(T&, SampleInfo&)
do. See the on-line docs on the DataReader here.Note that the variants of read that retuern a LoanedSamples<T> are getting the samples from the cache without doing a copy which for large samples can be more efficient that the versions that take the sample as an output parameter as these have no choice but make a copy.
Note also that the
read()
operations leave the samples in the DataReader cache so you can access them again later. If you do not want that you should use thetake()
operations instead.Gerardo