edit : hmm, the float version just caused 28 errors with a different random seed. There could be some issue with how the numbers are checked or there genuinely were errors during the execution.
Obviously anecdotal evidence but the following does not cause the error count to become non-zero. Tested on Visual Studio 2017, release build for 64 bit. Your mileage may vary. Maybe it doesn't work with 32 bit builds, for example.
Code:
template<typename FloatType>
void test_threadloadstore(int64_t iterations)
{
std::mt19937 gen;
FloatType minlimit = 0.1;
FloatType maxlimit = 0.9;
std::uniform_real_distribution<FloatType> dist(minlimit, maxlimit);
FloatType observe = 0.5;
int64_t errors = 0;
auto task = [&observe,&errors, iterations, minlimit, maxlimit]()
{
for (int64_t i = 0; i < iterations; ++i)
{
FloatType x = observe;
if ((x<minlimit || x>=maxlimit) || std::fpclassify(x)!=FP_NORMAL)
++errors;
}
};
std::thread th(task);
for (int64_t i = 0; i < iterations; ++i)
{
FloatType x = dist(gen);
observe = x;
}
th.join();
std::cout << errors << " errors when reading value\n";
}
int main()
{
test_threadloadstore<float>(1000000000);
test_threadloadstore<double>(1000000000);
}
If it's not clear from the code itself, it repeatedly generates random numbers in the main thread within a given range and writes those into a variable. The other launched thread repeatedly checks that same variable that it is within the expected bounds and is not a "non-normal" number.