Hello,

I have found a behaviour of MinGW-W64 5.3.0 that disturbs me very much: the 
results of floating-point operations may differ, if they are run on different 
threads. This does not happen with Visual Studio 2015 and not with g++ 4.9.2 on 
my Debian Linux.

Please consider the following program:

-------------------------------------------------
#include <iostream>
#include <future>
#include <cmath>
#include <string>
#include <thread>

std::string getResult()
{
        std::cout << "Thread id: " << std::this_thread::get_id() << std::endl;
        std::string result = "Result:\n";

        double a, b, c;
        int i;
        for (i = 0, a = 1.0; i < 10000000; i++)
        {
                a *= 1.00000001;
                b = sqrt(a);
                c = pow(a, 0.5);
                if (b == c)
                        continue;
                result += std::string("a: ") + std::to_string(a) + " b: "
                                + std::to_string(b) + " c: " + 
std::to_string(c) + "\n";
        }

        return result;
}

int main()
{
        std::string string1 = getResult();

        std::future<std::string> result = std::async(std::launch::async, 
getResult);

        std::string string2 = result.get();

        if (string1 != string2)
        {
                std::cout << "String 1 is: " << std::endl << string1 << 
std::endl
                                << " and string 2 is: " << std::endl << string2 
<< std::endl;
        }
        else
        {
                std::cout << "The results are the same." << std::endl;
        }

        return 0;
}
-------------------------------------------------

If I compile it with g++ -O0 -std=gnu++11 Main.cpp the output is

-------------------------------------------------
Thread id: 1
Thread id: 2
The results are the same.
-------------------------------------------------

If I compile it with g++ -O1 -std=gnu++11 Main.cpp the output is
-------------------------------------------------
Thread id: 1
Thread id: 2
String 1 is: ...
-------------------------------------------------

The same happens with "-O2".

If I change the line
        std::future<std::string> result = std::async(std::launch::async, 
getResult);
to
        std::future<std::string> result = std::async(getResult);
then the result is independent from the optimization flag the following:

-------------------------------------------------
Thread id: 1
Thread id: 1
The results are the same.
-------------------------------------------------

What actually disturbs me, is that the results differ with asynchronous 
execution and optimization turned on. I tried parameters like -ffloat-store 
-msse2 -mfpmath=sse to have consistent floating-point operation results, but 
this won't help.

Is this a bug? Or are there compiler flags that force the floating-point 
operation results to be consistent?

Any help is greatly appreciated!

Regards,
Benjamin

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to