I have a double value that I need to access to inside a backgroundThread. I would like to use somethink like AtomiccmpExchange
but seam to not work with double. is their any other equivalent that I can use with double ? I would like to avoid to use Tmonitor.enter
/ Tmonitor.exit
as I need something the most fast as possible. I m under android/ios so under firemonkey
You could type cast the double
values into UInt64
values:
PUInt64(@dOld)^ := AtomicCmpExchange(PUInt64(@d)^,PUInt64(@dNew)^,PUInt64(@dComp)^);
Note that you need to align the variables properly, according to platforms specifications.
As @David pointed out, comparing double
values is not the same as comparing UInt64
values. There are some specific double values that will behave out of the ordinary:
A NaN
is normally (as specified in IEEE-754) detected by comparing a value by itself.
IsNaN := d <> d;
footnote: Delphi default exception handler is triggered in the event of comparing a NaN, but other compilers may behave differently. In Delphi there is an IsNaN()
function to use instead.
Likewise the value zero could be both positive and negative, for a special meaning. Comparing double 0
with double -0
will return true, but comparing the memory footprint will return false.