I have a server application that uses "a lot" of threads. Without wanting to get into an argument about how many threads it really should be using, it would be nice to be able to see some descriptive text in the debugger "threads" window describing what each one is, without having to click through to it and determine from the context what it is.
我有一个使用“很多”线程的服务器应用程序。不想进入关于它应该使用多少线程的争论,最好能够在调试器“线程”窗口中看到一些描述性文本,描述每个线程的内容,而不必点击它并且从上下文中确定它是什么。
They all have the same start address so generally the threads window says something like "thread_base::start" or something similar. I'd like to know if there is an API call or something that allows me to customise that text.
它们都具有相同的起始地址,因此通常线程窗口会显示类似“thread_base :: start”或类似内容的内容。我想知道是否有API调用或允许我自定义该文本的东西。
3 个解决方案
#2
Here is the code I use.
这是我使用的代码。
This goes in a header file.
这是一个头文件。
#pragma once
#define MS_VC_EXCEPTION 0x406d1388
#pragma warning(disable: 6312)
#pragma warning(disable: 6322)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // must be 0x1000
LPCSTR szName; // pointer to name (in same addr space)
DWORD dwThreadID; // thread ID (-1 caller thread)
DWORD dwFlags; // reserved for future use, most be zero
} THREADNAME_INFO;
inline
void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName)
{
#ifdef _DEBUG
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = szThreadName;
info.dwThreadID = dwThreadID;
info.dwFlags = 0;
__try
{
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(DWORD), (DWORD *)&info);
}
__except (EXCEPTION_CONTINUE_EXECUTION)
{
}
#else
dwThreadID;
szThreadName;
#endif
}
Then I call it like this inside the threads proc.
然后我在线程proc中调用它。
SetThreadName(GetCurrentThreadId(), "VideoSource Thread");
It is worth noting that this is the exact code that David posted a link to (Thanks! I had forgotten where I got it). I didn't delete this post because I'd like the code to still be available if MSDN decides to reorganize its links (again).
值得注意的是,这是大卫发布链接的确切代码(谢谢!我忘记了我得到它的地方)。我没有删除这篇文章,因为如果MSDN决定重新组织其链接(我再次),我希望代码仍然可用。
#3
Windows 10 added SetThreadDescription(), on that platform this will be the best method.
Windows 10添加了SetThreadDescription(),在该平台上,这将是最好的方法。
#1
Use SetThreadName
#2
Here is the code I use.
这是我使用的代码。
This goes in a header file.
这是一个头文件。
#pragma once
#define MS_VC_EXCEPTION 0x406d1388
#pragma warning(disable: 6312)
#pragma warning(disable: 6322)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // must be 0x1000
LPCSTR szName; // pointer to name (in same addr space)
DWORD dwThreadID; // thread ID (-1 caller thread)
DWORD dwFlags; // reserved for future use, most be zero
} THREADNAME_INFO;
inline
void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName)
{
#ifdef _DEBUG
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = szThreadName;
info.dwThreadID = dwThreadID;
info.dwFlags = 0;
__try
{
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(DWORD), (DWORD *)&info);
}
__except (EXCEPTION_CONTINUE_EXECUTION)
{
}
#else
dwThreadID;
szThreadName;
#endif
}
Then I call it like this inside the threads proc.
然后我在线程proc中调用它。
SetThreadName(GetCurrentThreadId(), "VideoSource Thread");
It is worth noting that this is the exact code that David posted a link to (Thanks! I had forgotten where I got it). I didn't delete this post because I'd like the code to still be available if MSDN decides to reorganize its links (again).
值得注意的是,这是大卫发布链接的确切代码(谢谢!我忘记了我得到它的地方)。我没有删除这篇文章,因为如果MSDN决定重新组织其链接(我再次),我希望代码仍然可用。
#3
Windows 10 added SetThreadDescription(), on that platform this will be the best method.
Windows 10添加了SetThreadDescription(),在该平台上,这将是最好的方法。