我正在尝试使用 boost::wait_condition 使线程休眠,直到有一些新数据可用。我的功能简化为:
bool Node::waitForNewData() const
{
boost::unique_lock<boost::mutex>(mWasNotifiedMutex);
mWasNotified = false;
while (true)
{
if (mWasNotified)
return true;
if (mThreadIsRequestedToStop)
return false;
mWasNotifiedWaitCondition.wait(mWasNotifiedMutex);
}
}
Boost 正在从 wait() 函数中抛出异常,消息为:
boost unique_lock has no mutex: Operation not permitted
我正在使用这样的函数来通知等待条件:
void Node::callbackNewDataArrived()
{
{
boost::unique_lock<boost::mutex>(mHasNewInletDataMutex);
mWasNotified = true;
}
mWasNotifiedWaitCondition.notify_all();
}
以及 header 中的这些声明:
class Node
{
// ...
mutable bool mWasNotified;
mutable boost::mutex mWasNotifiedMutex;
mutable boost::condition_variable mWasNotifiedWaitCondition;
std::atomic<bool> mThreadIsRequestedToStop;
};
我在 Xcode 4.6.2 中构建,并在 OSX 10.8.5 上启用了 c++11 支持。我的 boost 库是用
./b2 toolset=clang cxxflags="-std=c++11 -stdlib=libc++ -arch i386 -arch x86_64" macosx-version=10.6 linkflags="-stdlib=libc++" --prefix=/usr/local -j 10 define=BOOST_SYSTEM_NO_DEPRECATED stage release
我链接到的 boost 库是
libboost_chrono.a
libboost_date_time.a
libboost_filesystem.a
libboost_system.a
libboost_thread.a
知道我在这里做错了什么吗?
最佳答案
boost::unique_lock<boost::mutex>(mWasNotifiedMutex);
这声明了一个名为 mWasNotifiedMutex
的空锁,隐藏了互斥锁本身。您打算使用互斥锁来初始化锁:
boost::unique_lock<boost::mutex> lock(mWasNotifiedMutex);
然后您需要将它而不是互斥量提供给条件变量:
mWasNotifiedWaitCondition.wait(lock);
关于c++ - 尝试等待 boost::condition_Variable 时出现 "unique_lock has no mutex: Operation not permitted"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19501740/