SetThreadName not working with Visual Studio 2005

允我心安 提交于 2019-12-20 07:27:33

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/2448155/setthreadname-not-working-with-visual-studio-2005

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!