I use boost::thread_groupto create (using
thread_group::create_thread()) and send streams. To limit the maximum thread numbers, at the end of each thread, I delete the thread from thread_groupand delete the thread itself (so that I can decide whether to create new threads). However, it hangs somewhere between the creation and deletion of the last thread (say, the 999th of 999 people).
My questions:
- OK, to remove the stream from within myself, like what am I doing? if not, what is the best way to achieve this
- Why is my code freezing?
Below is the code:
// 1- code for creating and sending a stream
{
boost::mutex::scoped_lock lk(m_mutex_for_ptr);
boost::thread* p = m_thread_group.create_thread(boost::bind(
&detectiveT<equal_predicate>::f,
this,
duplicate_hashes
));
m_thread_ptrs.insert(make_pair(p->get_id(), p));
cout << "thread created: "
<< p->get_id() << ", "
<< m_thread_group.size() << ", " m_thread_ptrs.size() <<
"\n";
}
// 2- thread execution code
void f(list<map_iterator_type>& l)
{
Do_something(l);
boost::this_thread::at_thread_exit(boost::bind(
&detectiveT<equal_predicate>::remove_this_thread,
this
));
}
// 3- code to delete the stream itself
void remove_this_thread()
{
{
boost::mutex::scoped_lock lk(m_mutex_for_ptr);
boost::thread::id this_id(boost::this_thread::get_id());
map<boost::thread::id, boost::thread*>::iterator itr;
itr = (m_thread_ptrs.find(this_id));
if(m_thread_ptrs.end() != itr)
{
m_thread_group.remove_thread(itr->second);
delete itr->second;
m_thread_ptrs.erase(this_id);
cout << "thread erased: "
<< this_id << ", "
<< m_thread_group.size() << ", "
<< m_thread_ptrs.size() << "\n";
}
}
}
source
share