#include <iostream>
#include <thread>
int x = 0;
int y = 0;
void f()
{
std::cout <<"f called\n";
static int c = 0;
while(y == 0)
{
++c;
}
std::cout << "c=" << c << std::endl;
std::cout << "x=" << x << std::endl;
}
void g()
{
std::cout <<"g called\n";
x = 42;
y = 1;
}
int main()
{
std::thread t1(f);
std::thread t2(g);
t1.join();
t2.join();
return 0;
}
f is supposed to print 'x=42' when the flag y is set from the other thread (well, it cal also print x=0, but that's not the issue here)
when running in debug mode, it works as expected:
f called
g called
c=80213
x=42
but in release mode, the second thread seems freezing and the program never end:
f called
g called
could someone please explain why?
PS. The program was compiled with mignw g++ 4.8.0