<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to 7: Eventual pthread resource exhaustion uncaught exception</title><link>https://sourceforge.net/p/threadpool/bugs/7/</link><description>Recent changes to 7: Eventual pthread resource exhaustion uncaught exception</description><atom:link href="https://sourceforge.net/p/threadpool/bugs/7/feed.rss" rel="self"/><language>en</language><lastBuildDate>Thu, 10 May 2012 11:17:44 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/threadpool/bugs/7/feed.rss" rel="self" type="application/rss+xml"/><item><title>Eventual pthread resource exhaustion uncaught exception</title><link>https://sourceforge.net/p/threadpool/bugs/7/</link><description>&lt;div class="markdown_content"&gt;&lt;p&gt;(Affects pthreads i.e. Linux implementation of boost)&lt;br /&gt;
In a situation where pools are being continually created and destroyed, the terminate_all_workers() doesnt properly join all the threads that were created by the pool.&lt;br /&gt;
As a result, eventually pthread_create() will fail inside the  worker_thread&amp;lt;pool_type&amp;gt;::create_and_attach() &lt;br /&gt;
Because this is silent, instead, the next pool created deadlocks waiting for a condition that will never be sent because there was no thread created to send it but the pool doesnt realise this because it doesnt properly detect creation errors.&lt;/p&gt;
&lt;p&gt;On a linux x86-64 OpenSuse 11.4 with intel 8 core CPU, this occurs after 2752 unjoined threads exist.&lt;br /&gt;
On a linux amd64 phenom quad core debian squeeze, there seems to have been 32768 unjoined threads created&lt;/p&gt;
&lt;p&gt;There are two problems:&lt;br /&gt;
(a) the resource exeption within create_and_attach() is silently trapped. This caused me a lot of head scratching for a while...&lt;br /&gt;
(b) the terminate_all_workers method() does not join all exiting threads, because it runs before the thread destruction hook worker_destructed() has a chance to add the thread to the terminated_workers vector &lt;/p&gt;
&lt;p&gt;I resolved this by counting the number of times the thread creation is called for the pool, and using that to make sure that many threads are waiting to be cleaned up before exiting.&lt;/p&gt;
&lt;p&gt;Update: after I fixed this problem, I read bug 2910301 and suspect it is the same problem. I also think  the fix supplied is just as valid as what worked for me.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;I would still class eating the resource exception as an additional bug, the caller should know if it is not possible to create the pool in the desired manner.&lt;/p&gt;
&lt;p&gt;To demonstrate the problem using unpatched 0.2.5:&lt;/p&gt;
&lt;p&gt;(Run the example)&lt;br /&gt;
g++ -I threadpool-0_2_5-src/threadpool/ -lboost_thread -lpthread example.cpp&lt;br /&gt;
./a.out&lt;/p&gt;
&lt;p&gt;After 172 iterations thread creation failed as 2562 threads were finished but never joined&lt;br /&gt;
because the exception is silently caught, output of 'task' stops after 172&lt;br /&gt;
when I inserted 'throw' in the handler, instead after 172 iterations the program aborted&lt;/p&gt;
&lt;p&gt;On a different computer, amd64 debian squeeze, I managed 2047 loops instead&lt;/p&gt;
&lt;p&gt;After my patch, this runs to completion&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andrew McDonnell</dc:creator><pubDate>Thu, 10 May 2012 11:17:44 -0000</pubDate><guid>https://sourceforge.net30602a74c69a14393eb0e148470425b9be05ef86</guid></item></channel></rss>