我正在尝试使用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
知道我在这里做错了吗?
答案 0 :(得分:5)
boost::unique_lock<boost::mutex>(mWasNotifiedMutex);
声明了一个名为mWasNotifiedMutex
的空锁,隐藏了互斥锁本身。您打算使用互斥锁来初始化锁:
boost::unique_lock<boost::mutex> lock(mWasNotifiedMutex);
然后你需要给条件变量而不是互斥量:
mWasNotifiedWaitCondition.wait(lock);
答案 1 :(得分:0)
也许您忘了链接到pthread
:
-lpthread