By my calculations, it should create the separate thread running Loop(), wait for user input, and when the user presses enter, break the loop (aka. display the "Broken!" message box). However, nothing seems to happen. The more bizarre part is that if I add something like:
inside the while(1) loop, it suddenly and magically starts working perfectly: it couts . all over the screen, but when I press enter, it displays the "Broken!" message, which is what I want.
Now why would that happen? How is anything related to what is executed inside the threaded function?
Seems to be working. Thank you.
What was I doing wrong? I know the "volatile" prevents optimization, so perhaps somehow the loop was being modified (erasing the if(BreakIt)break; line since the compiler assumed BreakIt will never change state perhaps?), but why did the cout call make things work?
[Edit] Since we're here, is it possible to call a non-dword(void*) function from inside a class in a separate thread? I tried accomplishing that using a wrapper, but it produces a segmentation fault. I mean something like:
Yes, it's the optization. I guess that the optimization wasn't worth doing when the cost of the cout call had to be met.
But as the wait loop you're using is something that should never occur in real code, this particular problem is just a curiousity. Spinning an empty loop is potentially very costly in terms of CPU, so you would use a wait function in practice.
Using a class for the thread is better. This code is just for illustrative purposes.