我正在尝试使用 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();
}
以及标题中的这些声明:
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
知道我在这里做错了什么吗?