スレッドのboost::thread_group
作成(使用
)とディスパッチに使用しています。thread_group::create_thread()
最大スレッド数を制限するために、各スレッドの最後で、からスレッドthread_group
を削除し、スレッド自体を削除します(新しいスレッドを作成する必要があるかどうかを判断できるようにするため)。ただし、最後のスレッドの作成と削除の間のどこかでハングします(たとえば、合計999の999番目のスレッド)。
私の質問は次のとおりです。
- 私のようにスレッド自体からスレッドを削除しても大丈夫ですか?そうでない場合、これを達成するための最良の方法は何ですか
- コードがハングするのはなぜですか?
関連するコードは次のとおりです。
//1-スレッドを作成してディスパッチするコード
{
//mutex for map<thread_id, thread*> operations
boost::mutex::scoped_lock lk(m_mutex_for_ptr);
// create a thread for this->f(duplicate_hashes)
boost::thread* p = m_thread_group.create_thread(boost::bind(
&detectiveT<equal_predicate>::f,
this,
duplicate_hashes
));
// save the <thread_id,thread pointer> map for later lookup & deletion
m_thread_ptrs.insert(make_pair(p->get_id(), p));
// log to console for debug
cout << "thread created: "
<< p->get_id() << ", "
<< m_thread_group.size() << ", " m_thread_ptrs.size() <<
"\n";
}
//2-スレッド実行のコード
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-スレッド自体を削除するコード
void remove_this_thread()
{
{
//mutex for map<thread_id, thread*> operations
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)
{
// remove it from the control of thread_group
m_thread_group.remove_thread(itr->second);
// delete it
delete itr->second;
// remove from the map
m_thread_ptrs.erase(this_id);
// log to console for debug
cout << "thread erased: "
<< this_id << ", "
<< m_thread_group.size() << ", "
<< m_thread_ptrs.size() << "\n";
}
}
}