Pedro.Alonso
Pedro.Alonso

Reputation: 1005

boost thread lock error

I have this code which is giving the error:

 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

Since I don't know where is the error I'll try to copy the code:

Main

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

Threads

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

The code is used in a alignment program I'm trying to do parallel, it aligns the tuples one by one in different threads, until the waiting list (vector wait) is empty. Thanks. I have done something I put a lock.unlock() before every if and else and its working now, but why??

Upvotes: 1

Views: 4830

Answers (1)

Paolo Brandoli
Paolo Brandoli

Reputation: 4750

You are locking the mutex twice.

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

locks the mutex for you. It will also unlock it when it goes out of scope, so you don't need to unlock it by yourself.

So the following lines are unnecessary:

lock.lock();
lock.unlock();

Upvotes: 2

Related Questions