This shouldn't be too hard to understand:
bool ThreadIsRunning = false;
void Thread1(void *nothing){
ThreadIsRunning = true;
cout << "The thread is running!\n";
Sleep(1000);
cout << "Ending thread!\n");
ThreadIsRunning = false;
_endthread();
return;
}
int main(){
std::cout << "The thread is starting!\n";
_beginthread(Thread1, 0, 0);
std::cout << "Waiting for thread to end!\n";
while(ThreadIsRunning);
std::cout << "The thread is ended!\n";
return 0;
}
So the main
thread wait's for the Thread1
to set ThreadIsRunning
to false, right?
Yeah, but it does not. Nothing happens when it goes to false. Shouldn't it check the value for eternity until it's changed?
It works if I put while(ThreadIsRunning) Sleep(10);
but I don't think it should be necessary for my code to work.
while(ThreadIsRunning) void();
does not work either.
Im using Visual Studio Ultimate 2012.
C/C++ command line options:
/GS /GL /analyze- /W3 /Gy /Zc:wchar_t /Zi /Gm- /O2 /sdl /Fd"Release\vc110.pdb" /fp:precise /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Gd /Oy- /Oi /MD /Fa"Release\" /EHsc /nologo /Fo"Release\" /Fp"Release\Test1.pch"
Linker command line options:
/OUT:"<projectsfolder>\Test1\Release\Test1.exe" /MANIFEST /LTCG /NXCOMPAT /PDB:"<projectsfolder>\Test1\Release\Test1.pdb" /DYNAMICBASE "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO /PGD:"<projectsfolder>\Test1\Release\Test1.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"Release\Test1.exe.intermediate.manifest" /OPT:ICF /ERRORREPORT:PROMPT /NOLOGO /TLBID:1
Edit:
There is NOT a schedule/race/time problem. Even if I add Sleep(1000);
after _beginthread(Thread1, 0, 0)
, the problem is still that nothing happens when ThreadIsRunning
goes to false