I have this really weird issue going on with threads ? this is the reduced code (I compiled it, and the problem is 100% reproducable)
int APIENTRY _tWinMain(In HINSTANCE hInstance,
In_opt HINSTANCE hPrevInstance,
In LPTSTR lpCmdLine,
In int nCmdShow)
{
bool b = false;
SDL_Thread * t = SDL_CreateThread(NIThreadProc, “2ndThread”, &b);
// ::Sleep(2000);
while (b == false)
{
}
MessageBox(NULL,L"b is now true\n",L"duh",MB_OK);
return 0;
}
int NIThreadProc(void ptr)
{
bool * ready =(bool) ptr;
::Sleep(100);
MessageBox(NULL, L"setting b to true\n", L"duh", MB_OK);
*ready = true;
return 1;
}
the expected behavior if the program is :
- main app launches
- starts 2nd thread
- 2nd thread waits 100msec then displays a message and sets a passed boolean to true
- main thread waits for that boolean, then displays a message
- application ends
BUT ?. unless I UNCOMMENT the sleep(2000) function, the main thread NEVER exits the while(b==false) loop !!! I see in the task manager that the task is still active (and using CPU), but the ?b is now true? message never appears.
if I do uncomment the sleep, the app works as expected.
also : this is only in release mode, not in debug mode
any ideas ?