我有这段代码:
void threaded_function(Model_factory &mf, ppa::Node *root)
{
boost::mutex::scoped_lock lock(result_mutex);
typedef vector<boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> >
::iterator traveling;
if(!running_jobs.empty())
{
cout << "size of running " << running_jobs.size() << endl;
boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple_to_check =
running_jobs.front();
running_jobs.pop();
cout << "poping this object from running_jobs" << tuple_to_check << endl;
cout << "new size of running " << running_jobs.size() << endl;
lock.unlock();
ppa::Node *tuplets_father = boost::get<0>(tuple_to_check);
ppa::Node *first_son = boost::get<1>(tuple_to_check);
ppa::Node *second_son = boost::get<2>(tuple_to_check);
bool is_this_done = boost::get<3>(tuple_to_check);
tuplets_father->start_alignment_new(&mf);
lock.lock();
cout << "size of the wait " << wait.size() << endl;
boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> new_tuple
= boost::make_tuple(tuplets_father, first_son, second_son, true);
wait.push_back(new_tuple);
cout << "pushing this object to waiting list" << new_tuple << endl;
无需滚动的空间
cout << "new size of the wait " << wait.size() << endl;
lock.unlock();
lock.lock();
for(traveling i = wait.begin(); i != wait.end(); i++)
{
if(boost::get<3>(*i) == true)
{
cout << "found in here pushing to running jobs " << *i << endl;
boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple = *i;
wait.erase(i);
running_jobs.push(tuple);
}
}
lock.unlock();
}
else
{
boost::this_thread::yield();
}
这是我输出的一部分:
found in here pushing to running jobs (0 0x1dd00000142 0xffffffff00000143 1)
found in here pushing to running jobs (0 0 0 66)
found in here pushing to running jobs (0 0 0x1e000000142 67)
found in here pushing to running jobs (0 0x1e100000142 0xffffffff00000143 1)
found in here pushing to running jobs (0 0 0 66)
found in here pushing to running jobs (0 0 0x1e400000142 67)
found in here pushing to running jobs (0 0x1e500000142 0xffffffff00000143 1)
found in here pushing to running jobs (0 0 0 66)
found in here pushing to running jobs (0 0 0x1e800000142 67)
found in here pushing to running jobs (0 0x1e900000142 0xffffffff00000143 1)
found in here pushing to running jobs (0 0 0 66)
found in here pushing to running jobs (0 0 0x1ec00000142 67)
它会永远存在,我想知道错误在哪里,逻辑是什么?很可能是的,但我可以使用一双新的眼睛,谢谢。
答案 0 :(得分:0)
您的代码通过调用
使vector::iterator i
无效
wait.erase(i);
然后它会增加无效i
并使用它来确定它是否应该突破for
循环。
您应该使用std::partition()
。整个for循环可以替换为:
traveling running_jobs =
std::partition( wait.begin(), wait.end(), is_not_running );
running_jobs.insert( running_jobs, wait.end() );
wait.erase( running_jobs, wait.end() );
is_not_running
将取决于您的创作。这也会降低从O(n 2 )到O(n)的复杂性。
答案 1 :(得分:0)
您应该使用分区解决方案,但如果您只想修改代码,则应更改for循环以正确增加i,即:
for(traveling i = wait.begin(); i != wait.end();)
...
if (...)
{
...
i = wait.erase(i);
...
}
else
{
i++;
}