Sample updates while looping loaned samples

2 posts / 0 new
Last post
Offline
Last seen: 6 years 7 months ago
Joined: 09/17/2015
Posts: 53
Sample updates while looping loaned samples

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

 

Gerardo Pardo's picture
Offline
Last seen: 1 week 6 days ago
Joined: 06/02/2010
Posts: 602

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 the do_stuff() can take a while you should call datareader.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:

wait_for_new_data();
for (  samples = datareader.read(); samples.length() > 0 ; samples = datareader.read(); ) {
     for ( s in samples) {
         do_stuff(s); //do_stuff runs a long time, several min.
     }
}  
 

You can also read one sample at a time. This is what DataReader::read(T&) and DataReader::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 the take() operations instead.

Gerardo