{"id":98145,"date":"2018-03-02T07:00:00","date_gmt":"2018-03-02T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=98145"},"modified":"2019-03-13T00:49:41","modified_gmt":"2019-03-13T07:49:41","slug":"20180302-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180302-00\/?p=98145","title":{"rendered":"If I call GetExitCodeThread for a thread that I know for sure has exited, why does it still say STILL_ACTIVE?"},"content":{"rendered":"<p>A customer reported that when they took the thread handle returned by the <code>_beginthread<\/code> function and passed it to <code>Get&shy;Exit&shy;Code&shy;Thread<\/code>, the function reported that the thread was <code>STILL_<\/code><code>ACTIVE<\/code>, even though the thread is known to have already exited. <\/p>\n<p>The customer found this text in <a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/kdzttdcb.aspx\">the documentation for <code>_beginthread<\/code><\/a> and wants more information about the case where the function can return an invalid handle if the thread exits quickly. <\/p>\n<blockquote CLASS=\"q\"><p> If the thread that&#8217;s generated by <b>_beginthread<\/b> exits quickly, the handle that&#8217;s returned to the caller of <b>_beginthread<\/b> might be invalid or point to another thread. <\/p><\/blockquote>\n<p>The point is that the <code>_begin&shy;thread<\/code> function returns a handle to a thread, but that handle is valid only as long as the thread is running. <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20170929-00\/?p=97115\">Once the thread exits, the handle spontaneously becomes invalid<\/a>, at which point it becomes eligible for recycling. <\/p>\n<p>The upshot of this is that the handle returned by <code>_begin&shy;thread<\/code> is useless, because it can go invalid for reasons outside your control. <\/p>\n<p>The source code for the C runtime library is included with Visual Studio. You can read the source code for the <code>_begin&shy;thread<\/code> function if you want to understand what&#8217;s going on. <\/p>\n<p>The customer asked whether this behavior of <code>_begin&shy;thread<\/code> would explain the problem they are seeing. <\/p>\n<p>Yes, it is exactly the problem you are seeing. Calling <code>Get&shy;Exit&shy;Code&shy;Thread<\/code> with the handle returned by <code>_begin&shy;thread<\/code> is useless. <\/p>\n<ul>\n<li>If the thread is running, then you will get     <code>STILL_<\/code><code>ACTIVE<\/code>.<\/li>\n<li>If the thread is not running, then you are using an invalid     parameter and you will get garbage.     The value <code>STILL_<\/code><code>ACTIVE<\/code> is one possible     manifestation of garbage.<\/li>\n<\/ul>\n<p>In general, the return value from <code>_begin&shy;thread<\/code> is not useful and should not be used for anything other than determining whether the thread was started successfully. <\/p>\n<p>We recommend that if the customer wants to use the thread handle, they should switch to <code>_begin&shy;thread&shy;ex<\/code> and remember to close the handle when they are done. <\/p>\n<p>The customer explained that their application was originally developed by another company. They had considered switching from <code>_begin&shy;thread<\/code> to <code>_begin&shy;thread&shy;ex<\/code>, but didn&#8217;t want to do so unless absolutely necessary, because they would have to justify the time and money required to fix the problem to their management. <\/p>\n<p>Yes, switching from <code>_begin&shy;thread<\/code> to <code>_begin&shy;thread&shy;ex<\/code> will fix the problem. As noted, the handle returned by <code>_begin&shy;thread<\/code> is useless, and any code that tries to do anything beyond test it for success is like to run into problems exactly like the one you describe. <\/p>\n<p>We learned about some people who <a HREF=\"https:\/\/technet.microsoft.com\/en-us\/library\/jj643252.aspx\">want documentation that a bad idea is a bad idea<\/a>. But that documentation is already there in MSDN. They already have the paperwork they need. I couldn&#8217;t quite figure out what the customer was looking for. Did they just want a personalized version of the documentation customized just for them? <\/p>\n<p>If that&#8217;s what you need, you can copy\/paste the following paragraph. <\/p>\n<blockquote CLASS=\"m\">\n<p>To whom it may concern, <\/p>\n<p>It is my recommendation that the return value from the <code>_begin&shy;thread<\/code> function should be used only to determine whether the function succeeded. In particular, the handle returned by the function is of indeterminate lifetime and cannot be reliably used. <\/p>\n<p>Sincerely, <\/p>\n<p>Raymond Chen <\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Because that&#8217;s not the thread handle you think it is.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-98145","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Because that&#8217;s not the thread handle you think it is.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=98145"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98145\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=98145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=98145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=98145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}