I am trying to adapt my program to use long double instead of double, in which I also use atomic numbers.
After the change I encountered something that looks like an infinite cycle in the std::atomic library.
Here is a sample code to illustrate the problem:
#include <atomic>
#include <iostream>
std::atomic<long double> my_long;
int main(){
long double my_temp = my_long;
while (!my_long.compare_exchange_weak(my_temp, my_temp + 5.0L)) {
my_temp = my_long;
}
std::cout << "Finished!" << std::endl;
return 0;
}
Now using atomic with types of non-standard sizes require to link against the atomic library, so the compile/link is successful, but the execution seems to be in an infinite cycle:
user@comp:/usr$ g++ atomic_test.cpp -latomic -std=c++14
user@comp:/usr$ ./a.out
^C
If the variable type is replaced from long double
to double
, there is no block, and the program executes as intended.
user@comp:/usr$ g++ atomic_test.cpp -latomic -std=c++14
user@comp:/usr$ ./a.out
Finished!
Why does this happen?
g++ version:
user@comp:/usr$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/6/lto-wrapper
Target: x86_64-linux-gnu
...
linux-gnu
Thread model: posix
gcc version 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)