How to: Set a Thread Name in Native Code
dddd
最近在分析webkit 项目 。分享其中几个tips!一、WTF库WTF的详细含义未找到出处,在webkit上找了找,大致是webkit 项目 继承使用的之前的一个封装库。其中包涵了数值转换,字符串,智能指针,线程管理,线程间同步等函数库。从源代码的命名来看,有多种移植版本,如以下注释
// For portability, we do not use thread-safe statics natively supported by some compilers (e.g. gcc).
#define AtomicallyInitializedStatic(T, name) \
WTF::lockAtomicallyInitializedStaticMutex(); \
static T name; \
WTF::unlockAtomicallyInitializedStaticMutex();
二、How to: Set a Thread Name in Native Code
MSDN的释义是To set a thread name in your program, use the SetThreadName function
原文见:http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.90).aspx
1//
2 // Usage: SetThreadName (-1, "MainThread");
3 //
4 #include <windows.h>
5 const DWORD MS_VC_EXCEPTION=0x406D1388;
6
7 #pragma pack(push,8)
8 typedef struct tagTHREADNAME_INFO
9 {
10 DWORD dwType; // Must be 0x1000.
11 LPCSTR szName; // Pointer to name (in user addr space).
12 DWORD dwThreadID; // Thread ID (-1=caller thread).
13 DWORD dwFlags; // Reserved for future use, must be zero.
14 } THREADNAME_INFO;
15 #pragma pack(pop)
16
17 void SetThreadName( DWORD dwThreadID, char* threadName)
18 {
19 THREADNAME_INFO info;
20 info.dwType = 0x1000;
21 info.szName = threadName;
22 info.dwThreadID = dwThreadID;
23 info.dwFlags = 0;
24
25 __try
26 {
27 RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
28 }
29 __except(EXCEPTION_EXECUTE_HANDLER)
30 {
31 }
32 }