I recently had to change code that was so badly written, I had to redo the entire module. I wasn't going to waste time trying to figure out all the twists and turns, when I already knew what the code was supposed to accomplish.
Although there are better ways to achieve the same effect.
If you assume the VC compiler (and some others) then marking the bool as volatile and making sure it's properly aligned would be sufficient.
if you can't assume a particular brand of compiler and a particular way it handles volatile (the spec guarantees nothing) you need explicit synchronisation. Even in that case, there's better ways around it.
But the whole idea here of creating a thread and then communicating with the main thread via timer messages makes it obvious that the guy that wrote this was just clueless on what he was actually trying to do.
Yes well, I posted the above code snippets for you all to have a laugh - at the moment I could do with one myself. The guys that wrote it have all long gone and until I started to look in detail at the program no one had a clue it was so badly coded (which says something about management, project control etc etc - further heads are rolling over this). We've now looking at other programs these guys had messed with to get an idea of how big a problem we really have.
I won't try to disect the code snippets much. I'm writing a completely new program to replace this mess from the ground up and this sort of mad programming won't feature. As far as I can tell, this code had two purposes. One was to check that memory wasn't being corrupted - which comes back to my original posting question re error with GlobalFree which started off this whole episode - and second was to make sure that the message loop in the main program wasn't waiting around for processing to complete whilst dealing with a message and hence not making the program responsive to user input/mouse etc. If a message took too long to process then the hearbeat would fail and an error message displayed. Practically every message in the main WndProc function that is processed within the program starts a new thread!!!! As I said before, I've never in all my years programming seen anything like it - and I hope I never will.