1

我有这个给出错误的代码:

 error this boost::lock_error
 error this boost::lock_error
 error this boost::lock_error
 error this boost::lock_error
 error this boost::lock_error

由于我不知道错误在哪里,我将尝试复制代码:

主要的

  boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool>  tuple = dups.back();
  ppa::Node *n = boost::get<0>(tuple);
  cout << "creating other threads" << endl;
  for ( int i = 0; i < 5; ++i )
  {
    cout << "making  thread " << i << endl;
    g.create_thread( boost::bind( threaded_function, boost::ref(mf), 
                                    boost::ref(n) ));
  }

线程

  void threaded_function(Model_factory &mf, ppa::Node *root)
  {
    try 
    {   
      while(true)
      {
        boost::mutex::scoped_lock lock(result_mutex);

        if(wait.empty())
        {
          lock.unlock();
          break;
        }
        else 
        {
          lock.lock();
          if(!running_jobs.empty())
          {
            cout << "vector wait size = " << wait.size()   << "\n";
            cout << "running size   = " << running_jobs.size() << "\n";
            cout << "done size  = " << done.size()   << "\n";
            boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple;
            tuple = running_jobs.back();
            running_jobs.pop_back();
            ...
          }
          ...
        }
        ...
      }
    }
    catch (boost::lock_error& le)
    {
      cout << "error this " << le.what() << endl;
    }
  }

该代码用于我尝试并行执行的对齐程序中,它在不同的线程中一一对齐元组,直到等待列表(向量等待)为空。谢谢。我做了一些事情,我lock.unlock()在每个 if 和 else 之前都放了一个,现在它正在工作,但是为什么?

4

1 回答 1

2

您正在锁定互斥锁两次。

boost::mutex::scoped_lock lock(result_mutex);

为您锁定互斥锁。当它超出范围时它也会解锁它,因此您不需要自己解锁它。

所以以下几行是不必要的:

lock.lock();
lock.unlock();
于 2012-07-23T07:53:23.293 回答