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

次に、invalid をインクリメントし、それを使用してループ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 に答える