"unique_lock has no mutex: Operation not permitted" error when attempting to wait on boost::condition_Variable

StackOverflow https://stackoverflow.com/questions/19501740

Вопрос

I'm trying to use boost::wait_condition to sleep a thread until some new data is available. My function reduces down to this:

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 is throwing an exception from the wait() function with the message:

boost unique_lock has no mutex: Operation not permitted

I'm using a function like this to notify the wait condition:

void Node::callbackNewDataArrived()
{
    {
        boost::unique_lock<boost::mutex>(mHasNewInletDataMutex);
        mWasNotified = true;
    }
    mWasNotifiedWaitCondition.notify_all();
}

and these declarations in the header:

class Node
{
    // ...
    mutable bool mWasNotified;
    mutable boost::mutex mWasNotifiedMutex;
    mutable boost::condition_variable mWasNotifiedWaitCondition;
    std::atomic<bool> mThreadIsRequestedToStop;
};

I'm building in Xcode 4.6.2 with c++11 support enabled on OSX 10.8.5. My boost libraries were built with

./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

and the boost libraries I'm linking to are

libboost_chrono.a
libboost_date_time.a
libboost_filesystem.a
libboost_system.a
libboost_thread.a

Any idea what I'm doing wrong here?

Это было полезно?

Решение

boost::unique_lock<boost::mutex>(mWasNotifiedMutex);

That declares an empty lock called mWasNotifiedMutex, hiding the mutex itself. You meant to use the mutex to initialise a lock:

boost::unique_lock<boost::mutex> lock(mWasNotifiedMutex);

Then you need to give that, rather than the mutex, to the condition variable:

mWasNotifiedWaitCondition.wait(lock);

Другие советы

Perhaps you forgot to link to pthread:

-lpthread
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top