SetThreadName不适用于Visual Studio 2005 [英] SetThreadName not working with Visual Studio 2005

查看:147
本文介绍了SetThreadName不适用于Visual Studio 2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SetThreadName 不使用Visual Studio 2005设置线程名称,如下所示:

  DWORD threadId; 
HANDLE handle = CreateThread(NULL,stackSize,ThreadFunction,
ThreadParam,CREATE_SUSPENDED,& threadId);
if(handle)
{

SetThreadName(threadId,NiceName);
ResumeThread(handle);
}

打开Threads窗口后,可以看到NiceName的名称ThreadFunction那里。其他工具(如Intel Parallel Inspector)会按预期的方式使用NiceName。




  • 上面的代码有问题吗?


  • 代码是否可以与Visual Studio 2008或2010版本一起使用?



解决方案

经过几个实验,我发现这是因为Visual Studio正在尝试聪明,当线程开始执行时,它会给自己一个名字。解决方法不是在线程实际启动之前尝试给名称进行线程,最简单的方法是从线程函数内部调用SetThreadName。



仍然有兴趣了解其他版本的Visual Studio是否显示相同的行为。


SetThreadName does not set thread name with Visual Studio 2005, when used as below:

DWORD threadId;
HANDLE handle = CreateThread(NULL, stackSize, ThreadFunction,
                             ThreadParam, CREATE_SUSPENDED, &threadId);
if (handle)
{

   SetThreadName(threadId, "NiceName");
   ResumeThread(handle);
}

After opening the Threads window, instead of NiceName I can see the name of the ThreadFunction there. Other tools (like Intel Parallel Inspector) use NiceName as expected.

  • Is something wrong with the code above?

  • Does the code work with Visual Studio 2008 or 2010 editions?

解决方案

After a few experiments I have found it is because the Visual Studio is trying to be smart and when the thread begins to execute it gives a name to itself. The workaround is not to try to give the name to thread before the thread has actually started, the easiest way how to achieve this is to call the SetThreadName from inside of the thread function.

Still I would be interested in knowing if other versions of Visual Studio show the same behaviour.

这篇关于SetThreadName不适用于Visual Studio 2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆