给线程取名
2015-07-16 13:11
459 查看
有的时候,为了调试方便,快速定位问题,我们会为线程取名。由于程序比较简单,我直接贴代码:
#include <iostream>
#include <process.h>
using namespace std;
const DWORD kVCThreadNameException = 0x406D1388;
typedef struct tagTHREADNAME_INFO {
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
void SetThreadName(DWORD thread_id, const char* name) {
// The debugger needs to be around to catch the name in the exception. If
// there isn't a debugger, we are just needlessly throwing an exception.
if (!::IsDebuggerPresent())
return;
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = name;
info.dwThreadID = thread_id;
info.dwFlags = 0;
__try {
RaiseException(kVCThreadNameException, 0, sizeof(info) / sizeof(DWORD),
reinterpret_cast<DWORD_PTR*>(&info));
}
__except (EXCEPTION_CONTINUE_EXECUTION) {
}
}
unsigned __stdcall SecondThreadFunc(void* pArguments) {
for (int i = 0; i < 100; i++) {
cout << "线程打印\n";
Sleep(1000);
}
_endthread();
return 1;
}
void Func() {
unsigned thread_id;
HANDLE handle_thread = (HANDLE)_beginthreadex(0, 0, &SecondThreadFunc, nullptr, 0, &thread_id);
SetThreadName(thread_id, "thread22222");
WaitForSingleObject(handle_thread, INFINITE);
cout << "线程结束\n";
CloseHandle(handle_thread);
}
int main(int argc, TCHAR * argv[]) {
Func();
return 0;
}
![](https://img-blog.csdn.net/20150716121644453?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
运行程序,查看线程,很容易就会看到自己创建的线程
#include <iostream>
#include <process.h>
using namespace std;
const DWORD kVCThreadNameException = 0x406D1388;
typedef struct tagTHREADNAME_INFO {
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
void SetThreadName(DWORD thread_id, const char* name) {
// The debugger needs to be around to catch the name in the exception. If
// there isn't a debugger, we are just needlessly throwing an exception.
if (!::IsDebuggerPresent())
return;
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = name;
info.dwThreadID = thread_id;
info.dwFlags = 0;
__try {
RaiseException(kVCThreadNameException, 0, sizeof(info) / sizeof(DWORD),
reinterpret_cast<DWORD_PTR*>(&info));
}
__except (EXCEPTION_CONTINUE_EXECUTION) {
}
}
unsigned __stdcall SecondThreadFunc(void* pArguments) {
for (int i = 0; i < 100; i++) {
cout << "线程打印\n";
Sleep(1000);
}
_endthread();
return 1;
}
void Func() {
unsigned thread_id;
HANDLE handle_thread = (HANDLE)_beginthreadex(0, 0, &SecondThreadFunc, nullptr, 0, &thread_id);
SetThreadName(thread_id, "thread22222");
WaitForSingleObject(handle_thread, INFINITE);
cout << "线程结束\n";
CloseHandle(handle_thread);
}
int main(int argc, TCHAR * argv[]) {
Func();
return 0;
}
运行程序,查看线程,很容易就会看到自己创建的线程
相关文章推荐
- java-模拟tomcat服务器
- C#多线程之Thread中Thread.IsAlive属性用法分析
- Android开发笔记之:如何安全中止一个自定义线程Thread的方法
- java thread start()和run()方法简析
- Java中Runnable和Thread的区别分析
- Android开发笔记之:Handler Runnable与Thread的区别详解
- C#多线程之Thread中Thread.Join()函数用法分析
- Mysql Error Code : 1436 Thread stack overrun
- MySQL错误Forcing close of thread的两种解决方法
- c#线程Thread示例
- Java之Thread的join方法实例
- java中thread线程start和run的区别
- JAVA多线程Thread和Runnable的实现
- java多线程编程之使用thread类创建线程
- python线程锁(thread)学习示例
- C#中Finally的一个不太常见的用法
- java并发编程简介
- Java Thread Dumps 日志分析
- Thead的yield和sleep的区别
- java线程系列---Runnable和Thread的区别