简体   繁体   中英

Find where a thread started in Visual Studio 2010 C++

So I'm supposed to fix a dead-lock without profiler (at least I didn't find any useful one) in an application that runs unbeliavable 283 threads according to VisualStudio. Most of the threads are called Thread _threadstartexThread _threadstartex , making it hard to see what they belong to.

图片描述

Therefore I decided that my first step is going to find where the threads were started and set some names for them, so I can distinguish boost and Qt internal threads and our application threads.

The question therefore is (in case someone forgot the title): How to find FILE and LINE NUMBER where thread seen in visual studio was started?

Years, years ago I once learned at Microsoft TechEd that you can set a thread name when Visual Studio is your debugger. The code to call in your program is:

typedef struct tagTHREADNAME_INFO
{
    DWORD   dwType;     // must be 0x1000
    LPCSTR  szName;     // pointer to name (in user addr space), UTF-8
    DWORD   dwThreadID; // thread ID (-1=caller thread)
    DWORD   dwFlags;    // reserved for future use, must be zero
} THREADNAME_INFO;

void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName)
{
    THREADNAME_INFO info;
    info.dwType = 0x1000;
    info.szName = szThreadName;
    info.dwThreadID = dwThreadID;
    info.dwFlags = 0;

#pragma warning(push)
#pragma warning(disable: 6312)  // Possible infinite loop: use of the constant EXCEPTION_CONTINUE_EXECUTION in the exception-filter expression of a try-except. Execution restarts in the protected block
#pragma warning(disable: 6322)  // Empty _except block
    __try {
        RaiseException(0x406D1388, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
    }
    __except (EXCEPTION_CONTINUE_EXECUTION) {
    }
#pragma warning(pop)
}

References: MSDN How to: Set a Thread Name in Native Code and How to: Set a Thread Name in Managed Code .

Maybe Visual Studio 2015 has some better debugging facilities that may help you? Microsoft has implemented a lot of enhancements, but I am not sure of one of them will help you with your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM