I’m developing C++ with Visual Studio 2010. How is code stepping designed to deal with creation of new threads?
If I am stepping through code and I spawn a new thread with CreateThread(), will I enter that thread? If not, why not?
Edit:
I am getting unpredictable results, even with breakpoints in the thread function. Sometimes my program exits before hitting a breakpoint in the thread function. I am just wondering about what gives rise to this behavior.
This depends.
If the method called by
CreateThreadever hits a breakpoint while you are stepping through the code that calledCreateThreadthen the debugger will switch to that breakpoint and thread. From then on (until you hit F5 again) doing step over instructions (F10) will occasionally alternate between the original thread and the one created byCreateThread.This is true for every thread which is created during a given break session. Once you hit F5 though and break again (via breakpoint or pause) everything resets and stepping will only step through the thread which was originally broken into.
Here’s an example app
If I put a breakpoint on the
printffunction in_tmainhit F5 and then use F10 to step after the breakpoint is hit I will never step into theThread2method.However if I put a breakpoint on both the entry point to
_tmainandThread2and then hit F5 things change. First I will hit the breakpoint in_tmainas expected. If I keep hitting F10 after that I will eventually hit the breakpoint inThread2. From then on I can keep hitting F10 and it will alternate between the two threads ever few steps.Note: This is not because I keep hitting the breakpoints. Both of the breakpoints are hit only once since they’re at the method entry point. It’s simply the debugger behavior to alternate between threads which have been explicitly broken into for a given stop in the debugger.