Note that there are some explanatory texts on larger screens.

plurals
  1. POboost.thread dead-lock and self-deletion
    primarykey
    data
    text
    <p>I am using <code>boost::thread_group</code> to create(using <code>thread_group::create_thread()</code>) and dispatch threads. In order to limit the max thread numbers, at the end of each thread, I remove the thread from the <code>thread_group</code> and delete the thread itself(so that I could decide whether new threads need to be created). However it hangs somewhere between the creation and deletion of the last thread (say the 999th one of 999 in total). </p> <p>My questions are: </p> <ul> <li>is it OK to delete the thread from within itself like what I do? if not, what the best way to achieve this</li> <li>why does my code hangs?</li> </ul> <p>Below are the related code:</p> <p>//1- code to create and dispatch thread </p> <pre><code> { //mutex for map&lt;thread_id, thread*&gt; operations boost::mutex::scoped_lock lk(m_mutex_for_ptr); // create a thread for this-&gt;f(duplicate_hashes) boost::thread* p = m_thread_group.create_thread(boost::bind( &amp;detectiveT&lt;equal_predicate&gt;::f, this, duplicate_hashes )); // save the &lt;thread_id,thread pointer&gt; map for later lookup &amp; deletion m_thread_ptrs.insert(make_pair(p-&gt;get_id(), p)); // log to console for debug cout &lt;&lt; "thread created: " &lt;&lt; p-&gt;get_id() &lt;&lt; ", " &lt;&lt; m_thread_group.size() &lt;&lt; ", " m_thread_ptrs.size() &lt;&lt; "\n"; } </code></pre> <p>//2- code of the thread execution </p> <pre><code>void f(list&lt;map_iterator_type&gt;&amp; l) { Do_something(l); boost::this_thread::at_thread_exit(boost::bind( &amp;detectiveT&lt;equal_predicate&gt;::remove_this_thread, this )); } </code></pre> <p>//3- code to delete the thread itself </p> <pre><code>void remove_this_thread() { { //mutex for map&lt;thread_id, thread*&gt; operations boost::mutex::scoped_lock lk(m_mutex_for_ptr); boost::thread::id this_id(boost::this_thread::get_id()); map&lt;boost::thread::id, boost::thread*&gt;::iterator itr; itr = (m_thread_ptrs.find(this_id)); if(m_thread_ptrs.end() != itr) { // remove it from the control of thread_group m_thread_group.remove_thread(itr-&gt;second); // delete it delete itr-&gt;second; // remove from the map m_thread_ptrs.erase(this_id); // log to console for debug cout &lt;&lt; "thread erased: " &lt;&lt; this_id &lt;&lt; ", " &lt;&lt; m_thread_group.size() &lt;&lt; ", " &lt;&lt; m_thread_ptrs.size() &lt;&lt; "\n"; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload