Boost C++ Libraries: Ticket #2739: shouldn't at_thread_exit work on the main thread? https://svn.boost.org/trac10/ticket/2739 <p> In the following example mycallable1 is not called at exit of the main thread. Shouldn't at_thread_exit work on the main thread? </p> <div class="wiki-code"><div class="code"><pre> <span class="cp">#include</span> <span class="cpf">&lt;boost/thread.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span> <span class="k">struct</span> <span class="n">mycallable1</span> <span class="p">{</span> <span class="kt">void</span> <span class="k">operator</span><span class="p">()()</span> <span class="k">const</span> <span class="p">{</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;mycallable1&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> <span class="p">};</span> <span class="p">};</span> <span class="k">struct</span> <span class="n">mycallable2</span> <span class="p">{</span> <span class="kt">void</span> <span class="k">operator</span><span class="p">()()</span> <span class="k">const</span> <span class="p">{</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;mycallable2&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> <span class="p">};</span> <span class="p">};</span> <span class="kt">void</span> <span class="nf">my_thread</span><span class="p">()</span> <span class="p">{</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;my_thread&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> <span class="n">mycallable1</span> <span class="n">x</span><span class="p">;</span> <span class="n">boost</span><span class="o">::</span><span class="n">this_thread</span><span class="o">::</span><span class="n">at_thread_exit</span><span class="p">(</span><span class="n">x</span><span class="p">);</span> <span class="p">}</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">mycallable2</span> <span class="n">x</span><span class="p">;</span> <span class="n">boost</span><span class="o">::</span><span class="n">this_thread</span><span class="o">::</span><span class="n">at_thread_exit</span><span class="p">(</span><span class="n">x</span><span class="p">);</span> <span class="n">boost</span><span class="o">::</span><span class="kr">thread</span> <span class="n">th</span><span class="p">(</span><span class="n">my_thread</span><span class="p">);</span> <span class="n">th</span><span class="p">.</span><span class="n">join</span><span class="p">();</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div></div><p> Output: my_thread mycallable1 </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2739 Trac 1.4.3 viboes Thu, 28 May 2009 20:10:59 GMT milestone changed https://svn.boost.org/trac10/ticket/2739#comment:1 https://svn.boost.org/trac10/ticket/2739#comment:1 <ul> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.39.0</span> → <span class="trac-field-new">Boost 1.40.0</span> </li> </ul> <p> The attached files contains the tests showing the bug and a patch solving the issue. The idea been use atexit for the main thread. </p> Ticket viboes Thu, 28 May 2009 20:31:10 GMT attachment set https://svn.boost.org/trac10/ticket/2739 https://svn.boost.org/trac10/ticket/2739 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test_ticket_2739.cpp</span> </li> </ul> Ticket viboes Thu, 28 May 2009 20:31:59 GMT attachment set https://svn.boost.org/trac10/ticket/2739 https://svn.boost.org/trac10/ticket/2739 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">2739.patch</span> </li> </ul> Ticket viboes Thu, 28 May 2009 20:45:00 GMT <link>https://svn.boost.org/trac10/ticket/2739#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2739#comment:2</guid> <description> <p> I have no modified nothing for window as I can not check it. Can someone apply the same modifications to the src/win32/thread.cpp file? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Fri, 29 May 2009 22:38:40 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2739#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2739#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/2739#comment:1" title="Comment 1">viboes</a>: </p> <blockquote class="citation"> <p> The attached files contains the tests showing the bug and a patch solving the issue. The idea been use atexit for the main thread. </p> </blockquote> <p> I have run the Thread tests and the patch introduce a regression on test_tss.cpp:test_tss. The reason is that the patch do not make a difference between the thread main and native threads created directly with pthread_create. So the patch is not usable. Is there a way to identify the thread main? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Vladimir Prus</dc:creator> <pubDate>Fri, 12 Jun 2009 09:06:09 GMT</pubDate> <title>severity changed https://svn.boost.org/trac10/ticket/2739#comment:4 https://svn.boost.org/trac10/ticket/2739#comment:4 <ul> <li><strong>severity</strong> <span class="trac-field-old">Showstopper</span> → <span class="trac-field-new">Problem</span> </li> </ul> <p> It's not apparent that this is showstopper bug, nor even regression. </p> Ticket cowwoc@… Mon, 26 Oct 2009 01:08:18 GMT <link>https://svn.boost.org/trac10/ticket/2739#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2739#comment:5</guid> <description> <p> Hi, </p> <p> I can confirm that this issue affects Boost 1.40 under Windows. Please fix it for this platform too! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Anthony Williams</dc:creator> <pubDate>Wed, 09 Jun 2010 08:51:00 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/2739#comment:6 https://svn.boost.org/trac10/ticket/2739#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> <p> No, at_thread_exit should not work on the main thread. </p> <p> If you call exit() then all threads are abruptly terminated without calling their exit handlers. </p> <p> Returning from main is defined to be equivalent to calling exit(), so all threads (including the main thread) are terminated without calling exit handlers. </p> <p> On POSIX, if you exit the main thread by calling pthread_exit() then the thread exit handlers should run. Similarly, on Windows you can use <a class="missing wiki">ExitThread</a>(). However, I would recommend using atexit or explicit calls, or even just normal destructors, since using pthread_exit() or <a class="missing wiki">ExitThread</a> will not run destructors for automatic_objects. </p> Ticket