    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: SetThreadName for Windows native programs</title>
        <link>https://members.accu.org/index.php/blogs/1316</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Blogs</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Blogs</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/blogs/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/blogs/c73/">Blogs</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;SetThreadName for Windows native programs</h1>
<p><strong>Author:</strong>&nbsp;Roger  M  Orr</p>
<p>
<strong>Date:</strong> 21 April 2007 21:23:33 +01:00 or Sat, 21 April 2007 21:23:33 +01:00</p>
<p><strong>Summary:</strong>&nbsp;[21-04-2007] How to name your threads when debugging C++ code on Windows</p>
<p><strong>Body:</strong>&nbsp;When I gave my presentation on debugging at ACCU I had several people asking me about how to name threads when debugging under Windows.
<p>
I originally found the technique described in a presentation about Visual Studio but it has since surfaced on MSDN as well - <br/> <a href="http://msdn2.microsoft.com/en-us/library/xcb2z8hs(vs.71).aspx">http://msdn2.microsoft.com/en-us/library/xcb2z8hs(vs.71).aspx</a>
<p>
The method is to generate a special runtime exception which allows the program to pass the name for the thread to the debugger.
<p>
Note that this only works with <i>some</i> windows debuggers, and obviously won't work if you attach the debugger to the program after the threads have started up.
<p>
I find this very useful when debugging programs with a lot of threads - I hope you will too!<br/>
Does anyone know similar techniques for other systems - gdb on Linux for example?
<pre>
<code>
/**
 * SetThreadName
 *
 * Provide a text name for threads in the debugger.
 * Usage: SetThreadName (-1, &quot;MainThread&quot;);
 */
void SetThreadName( DWORD dwThreadID, LPCSTR szThreadName )
{
   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;

   THREADNAME_INFO info;
   info.dwType = 0x1000;
   info.szName = szThreadName;
   info.dwThreadID = dwThreadID;
   info.dwFlags = 0;

   __try
   {
      RaiseException( 0x406D1388, 0,
                      sizeof(info)/sizeof(DWORD),
                      (DWORD*)&amp;info );
   }
   __except(EXCEPTION_CONTINUE_EXECUTION)
   {
   }
}

</code>
</pre></p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
