{"id":245,"date":"2009-09-12T23:19:15","date_gmt":"2009-09-12T17:49:15","guid":{"rendered":"http:\/\/techtwaddle.net\/?p=245"},"modified":"2011-04-12T23:19:36","modified_gmt":"2011-04-12T17:49:36","slug":"closehandlecreatethread","status":"publish","type":"post","link":"https:\/\/techtwaddle.co.in\/blog\/2009\/09\/12\/closehandlecreatethread\/","title":{"rendered":"CloseHandle(CreateThread(&#8230;));"},"content":{"rendered":"<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">I was browsing through some code when I came across a line of code which said:<\/span><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128); font-style: italic;\">CloseHandle( CreateThread(NULL, 0, PeculiarThreadProc, NULL, 0, NULL) );<\/span><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Comic Sans MS;\">And I thought, wait a minute, is he closing the handle to the thread immediately after its created. I pinged my TL and he said the thread will continue to run until it terminates. I thought I had read in documents that a thread will terminate when the last handle to it was closed and here it was the last and only handle to the thread. A closer look at the documentation made it clear,<\/span><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Comic Sans MS;\">&quot;<span style=\"font-family: Verdana; font-style: italic; color: rgb(51, 153, 102);\">The thread object remains in the system until the thread has  terminated <\/span><\/span><span style=\"font-weight: bold; font-family: Verdana; font-style: italic; color: rgb(51, 153, 102);\">and<\/span><span style=\"font-family: Comic Sans MS;\"><span style=\"font-family: Verdana; font-style: italic; color: rgb(51, 153, 102);\"> all handles to it are closed through a call to Closehandle.<\/span>&quot;<\/span><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Comic Sans MS;\">I had missed the &#8216;and&#8217;. Anyways, to kill my curiosity for good I went ahead and tested the following code,<\/span><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">int WINAPI WinMain(HINSTANCE hInstance,<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HINSTANCE hPrevInstance,<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LPTSTR&nbsp;&nbsp;&nbsp; lpCmdLine,<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nCmdShow)<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">{<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; CloseHandle( CreateThread(NULL, 0, PeculiarThreadProc, NULL, 0, NULL));<\/span><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp; while(1)<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp; {<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sleep(1000);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp; }<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; return 0;<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">}<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DWORD WINAPI PeculiarThreadProc(void *lPtr)<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">{<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; DWORD tick;<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; while(1)<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; {<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; tick = GetTickCount();<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf(&quot;Tick count:%u\\r\\n&quot;, tick);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Sleep(1000);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; }<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">}<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Comic Sans MS;\">And as expected the thread kept running in all its glory.<\/span><\/p>\n<p><span style=\"font-family: Comic Sans MS;\">Suppose we modify our program a little as below,<\/span><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\"><br \/>\nint WINAPI WinMain(HINSTANCE hInstance,<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HINSTANCE hPrevInstance,<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LPTSTR&nbsp;&nbsp;&nbsp; lpCmdLine,<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nCmdShow)<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">{<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; CreateThread(NULL, 0, PeculiarThreadProc, NULL, 0, NULL);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp; while(1)<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp; &nbsp;&nbsp; {<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/do nothing<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sleep(1000);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp;&nbsp; }<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; return 0;<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">}<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DWORD WINAPI PeculiarThreadProc(void *lPtr)<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">{<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf(&quot;PeculiarThreadProc&quot;);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Sleep(100);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">}<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<br \/>\n<span style=\"font-family: Comic Sans MS;\">So in this case, the thread terminates but the handle to the thread is not closed. So does that mean that the thread object remains in the system? The answer is yes, and the thread object will exist in signaled state. And of course, if the main thread itself exits then all hope is lost (;<\/p>\n<p><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I was browsing through some code when I came across a line of code which said: CloseHandle( CreateThread(NULL, 0, PeculiarThreadProc, NULL, 0, NULL) ); And I thought, wait a minute, is he closing the handle to the thread immediately after its created. I pinged my TL and he said the thread will continue to run &hellip; <a href=\"https:\/\/techtwaddle.co.in\/blog\/2009\/09\/12\/closehandlecreatethread\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">CloseHandle(CreateThread(&#8230;));<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ktFF-3X","_links":{"self":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/245"}],"collection":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":1,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":246,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/245\/revisions\/246"}],"wp:attachment":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}