here's my code for sending messages every 3 seconds for 10 times. but it ignores all of if statements in while(true)
double current;
double freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
QueryPerformanceCounter((LARGE_INTEGER*)¤t);
float totalTime = 0.f;
float counter = 0.f;
while (true)
{
double previous = current;
QueryPerformanceCounter((LARGE_INTEGER*)¤t);
double deltaTime = (current - previous) / freq;
totalTime += deltaTime;
counter += deltaTime;
if (counter > 3.f)
{
cout << "msg Sent" << totalTime << "\n";
counter = 0;
}
if (totalTime > 30.f)
{
break;
}
}
weird thing is that if I print out two of those values in the middle, it works fine.
double current;
double freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
QueryPerformanceCounter((LARGE_INTEGER*)¤t);
float totalTime = 0.f;
float counter = 0.f;
while (true)
{
double previous = current;
QueryPerformanceCounter((LARGE_INTEGER*)¤t);
double deltaTime = (current - previous) / freq;
totalTime += deltaTime;
counter += deltaTime;
cout << totalTime << "\n";
cout << counter << "\n";
if (counter > 3.f)
{
cout << "msg Sent" << totalTime << "\n";
counter = 0;
}
if (totalTime > 30.f)
{
break;
}
}
I think it happened because of compiler optimization. if I'm right, is there any way to stop compiler messing up my code?
LARGE_INTEGER current;
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(¤t);
float totalTime = 0.f;
float counter = 0.f;
while (true)
{
LARGE_INTEGER previous = current;
QueryPerformanceCounter(¤t);
double deltaTime = (current.QuadPart - previous.QuadPart) / static_cast<double>(freq.QuadPart);
totalTime += deltaTime;
counter += deltaTime;
if (counter > 3.f)
{
cout << "msg Sent" << totalTime << "\n";
counter = 0;
}
if (totalTime > 30.f)
{
break;
}
}
This time I followed c++ rule and the result is same. So it's not the trouble of casting. and it also works when i put two prints in the middle.