I am working on a multi-threaded C++ program which deals with a lot of synchronization issues. I am using Visual Studio 2008.
The run-time behavior of my program (the order in which statements are executed across threads) seems to change when I debug it using breakpoints. Can this be explained? What is the concept at play here? I would expect the order of execution to remain the same.
Second question – if Thread1 is blocked by, say, a wait function call. Thread2 has statements waiting to be executed, in ready state. Is there any situation where the program will wait for Thread1 to proceed rather than giving execution to Thread2? I have removed all dependencies between the two thread and ensured that Thread2 is not waiting for any resource.
Appreciate the responses.
This article on multithreaded debugging techniques makes a few good summary points on the topic:
…and more to your specific latter question, it’s important to understand that–in the majority case–the operating system is free to interrupt the execution of any of your threads whenever it pleases, even ones that are “ready” to execute.