我有这个代码,它给出了错误:
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之前它现在正在工作,但为什么?
答案 0 :(得分:2)
您正在锁定互斥锁两次。
boost::mutex::scoped_lock lock(result_mutex);
为您锁定互斥锁。当它超出范围时它也会解锁它,所以你不需要自己解锁它。
所以不需要以下几行:
lock.lock();
lock.unlock();