【问题标题】:boost thread compiler error with GCC使用 GCC 提升线程编译器错误
【发布时间】:2010-10-11 23:10:58
【问题描述】:

在 linux,gcc 4.3 上,使用 boost::thread 实现和互斥体/条件变量编译一个类我得到以下奇怪的错误,显然是由于与 posix 线程库的类型冲突:

*Compiling: filter.cpp
/usr/include/boost/thread/condition.hpp: In member function »void boost::condition::wait(L&) [with L = boost::mutex]«:
/host/.../filter.cpp:68:   instantiated from here
/usr/include/boost/thread/condition.hpp:90: Error: no match für »operator!« in »!lock«*
*/usr/include/boost/thread/condition.hpp:90: Notice: candidates are: operator!(bool) <built in>*
*/usr/include/boost/thread/mutex.hpp:66: Error: »pthread_mutex_t boost::mutex::m_mutex« is private
/usr/include/boost/thread/condition.hpp:93: Error: in this context*

代码是:

void CFilter::process( CData **s )
{
    boost::mutex::scoped_lock bufferLock(m_mutex);
    while (!m_bStop)
        m_change.wait(bufferLock);                      //<- line 68

    // ... further processing
}

类声明

#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>

class CFilter
{
    // ...
    boost::shared_ptr<boost::thread> m_thread;
    boost::mutex m_mutex;
    boost::condition m_change;
    // ...

    process( CData **s );
}

操作符错误发生在boost的condition.hpp,在

if (!lock)
    throw lock_error();

我使用的是 Boost 1.38.0,在 Windows 上我没有发现任何问题。任何帮助表示赞赏!

【问题讨论】:

    标签: c++ linux gcc boost conditional-statements


    【解决方案1】:

    您必须等待bufferLock,而不是m_mutex

    while (!m_bStop)
        m_change.wait(bufferLock);
    

    Condition<>::wait()ScopedLock 作为参数,而不是Mutex

    【讨论】:

    • 没错,但并没有解决问题,编译错误依旧
    猜你喜欢
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多