C++ How to debug a thread?

This is a discussion on C++ How to debug a thread? within the C++ Programming forums, part of the General Programming Boards category; I am using MS Visual C/C++ and the program I am working on uses the CreateThread() routine to create five ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    C++ How to debug a thread?

    I am using MS Visual C/C++ and the program I am working on uses the CreateThread() routine to create five different threads. I am stepping through the debugger and noticed that I cannot get it to step into one of the threads, it only steps through the main portion of the program. How do I get the debugger to step into my thread which called 'wsnetprc.c'? I tried setting a break point in the thread program and it didn't recognize it.

    Any help or suggestions would be deeply appreciated.

    Thanks.

    MSA
    Go Angels!
    Go Lakers!
    Go Trojans!
    Go Stock Market!

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Debug->Threads.....you can suspend and switch from there

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    13

    __teneniel

    As you used CreateThread(/*...*/) you had to pass a entry function (your third parameter, i suppose).
    Just set a breakpoint within that thread entry function and VC will break anytime the function is executed. (You can set breakpoints almost everywhere)
    Anyway, if that doesn't suit you, there is in VC6 a menu item aviable while debugging. Within the topdown Debug, an item Threads opens a dialog showing all Threads of the currently debugged application. From there you can breaking right into every of these.
    In VC .net it's Debug-> Windows-> Threads.

    Hope that helps you
    Last edited by teneniel; 10-23-2002 at 01:35 PM.
    Teneniel
    "The Frenchmen and Russians possess the land, the British possess the sea, but we have over the airy realm of dreams command indisputably." ~ Heinrich Heine

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    didn't seem to work...

    It didn't seem to work. I step over the CreateThread() routine and no new thread seems to be running. The only one on the list is the WinMain thread.

    Any suggestions? This should be pretty straightforward????

    MSA
    Go Angels!
    Go Lakers!
    Go Trojans!
    Go Stock Market!

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    13

    Check the return value

    Are you sure that CreateThread() succeeded ?
    Check if the HANDLE you get as return value is not NULL.
    If it's NULL, call immediatelly after CreateThread() GetLastError(). Use Tools-> Error lookup to find out what the code returned by GetLastError() means;
    Code:
    SetLastError(0); // Just in case...
    if (!CreateThread(/*your params*/))
    {
        unsigned long err =  GetLastError(); // <- Break here and lookup  the value of err.
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. Replies: 2
    Last Post: 02-26-2009, 10:48 PM
  3. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 04:14 AM
  4. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  5. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21