2

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.

2
  • typically you don't step through in multi-threaded application, more likely you want to set breakpoints in the places of interest and suspend all threads when the breakpoint is hit. Commented Oct 28, 2011 at 17:09
  • Yes, I have tried it, see edits. Commented Oct 28, 2011 at 17:13

5 Answers 5

3

This depends.

If the method called by CreateThread ever hits a breakpoint while you are stepping through the code that called CreateThread then 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 by CreateThread.

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

DWORD WINAPI Thread2(LPVOID lParam)
{
    while (true)
    {
        printf("In Thread 2\n");
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    CreateThread( NULL, 0, &Thread2, NULL, 0, NULL);  
    while (true)
    {
        printf("In Thread 1\n");
    }
    return 0;
}

If I put a breakpoint on the printf function in _tmain hit F5 and then use F10 to step after the breakpoint is hit I will never step into the Thread2 method.

However if I put a breakpoint on both the entry point to _tmain and Thread2 and then hit F5 things change. First I will hit the breakpoint in _tmain as expected. If I keep hitting F10 after that I will eventually hit the breakpoint in Thread2. 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.

Sign up to request clarification or add additional context in comments.

Comments

2

No I do not think so. You will continue on the current thread until a breakpoint is hit in the other thread.

Comments

0

The execution will continue in the current thread until a breakpoint is hit. This breakpoint can be in a separate thread and execution will continue in that thread following the same rule.

Comments

0

No, you are not calling your thread method directly (that would defeat the purpose of the thread). You are requesting that the runtime execute your thread function in parallel with the current thread, starting as soon as possible.

If you don't have predefined breakpoints in all threads, I have seen MS debuggers bounce back and forth between threads when you choose "step over", but I can't say that I can tell if there is any predictability to it.

Comments

0

If I am stepping through code and I spawn a new thread with CreateThread(), will I enter that thread?

No, single stepping means you're stepping through the execution context of a particular thread. The debugger has several ways to switch which thread you're viewing. There's a Threads window which shows you information about all of the threads in the process. You could also set a breakpoint at the entry point for the new thread.

If not, why not?

Because the debugger doesn't know that CreateThread is a special function that launches a new thread. It just knows that you've asked it to step over the function call, and so it assumes you want to remain in the current thread. Also, depending on the scheduling, the new thread may or may not start right away. You could, for example, start the new thread in a paused state.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.