0

我有这个代码:

   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)

它将永远消失,我想知道错误在哪里,是否与逻辑有关?很可能是的,但我可以用一些新的眼睛,谢谢。

4

2 回答 2

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)。

于 2012-07-20T09:01:18.047 回答
0

您可能应该使用分区解决方案,但如果您只想修复您的代码,您应该更改您的 for 循环以正确递增 i,即:

for(traveling i = wait.begin(); i != wait.end();)
...
if (...)
{ 
    ...
    i = wait.erase(i);
    ...
}
else
{
     i++;
}
于 2012-07-20T09:55:33.287 回答