I made an atomic float, and it's probably not blazingly fast (that's ok), but its faster than wrapping a lock around a float, and it works, but I'm not sure if this is because of my good luck, or if this is actually thread safe. I think it is... but you never know, so I came to ask the experts :)
struct AtomicFloat: public tbb::atomic
{
float compare_and_swap(float value, float compare)
{
size_t value_ = tbb::atomic::compare_and_swap((size_t&)value,(size_t&)compare);
return reinterpret_cast(value_);
}
float operator=(float value)
{
size_t value_ = (*this).tbb::atomic::operator =((size_t&)value);
return reinterpret_cast(value_);
}
operator float()
{
return reinterpret_cast(*this);
}
float operator+=(float value)
{
volatile float old_value_, new_value_;
do
{
old_value_ = reinterpret_cast(*this);
new_value_ = old_value_ + value;
} while(compare_and_swap(new_value_,old_value_) != old_value_);
return (new_value_);
}
};
Also as a caveat I'm placing a static assert for size_of(float) == size_of(size_t).
Thanks!



