c++ - 使用 boost::scoped_lock(Mutex &mx, bool initially_locked) 构造函数给我错误

标签 c++ boost multithreading

不是有一个 boost::scoped_lock 构造函数将 bool 值作为第二个参数吗?我以为我以前用过它。 boost::scoped_lock(Mutex &mx, bool initially_locked)

documentation对于 scoped_lock,将第二个参数显示为“未指定”。任何人都知道 @(*$ 中的意思是什么?!?

我得到的错误是:

c:\program files\boost\boost_1_39\boost\thread\win32\basic_timed_mutex.hpp(118) : error C2679: binary '+' : no operator found which takes a right-hand operand of type 'const bool' (or there is no acceptable conversion)
        c:\program files\boost\boost_1_39\boost\date_time\posix_time\date_duration_operators.hpp(31): could be 'boost::posix_time::ptime boost::posix_time::operator +(const boost::posix_time::ptime &,const boost::gregorian::months &)' [found using argument-dependent lookup]
        c:\program files\boost\boost_1_39\boost\date_time\date_duration_types.hpp(132): or       'boost::gregorian::date boost::date_time::months_duration<base_config>::operator +(const boost::gregorian::date &,const boost::date_time::months_duration<base_config> &)' [found using argument-dependent lookup]
        with
        [
            base_config=boost::gregorian::greg_durations_config
        ]
        c:\program files\boost\boost_1_39\boost\date_time\posix_time\date_duration_operators.hpp(75): or       'boost::posix_time::ptime boost::posix_time::operator +(const boost::posix_time::ptime &,const boost::gregorian::years &)' [found using argument-dependent lookup]
        c:\program files\boost\boost_1_39\boost\date_time\date_duration_types.hpp(244): or       'boost::gregorian::date boost::date_time::years_duration<base_config>::operator +(const boost::gregorian::date &,const boost::date_time::years_duration<base_config> &)' [found using argument-dependent lookup]
        with
        [
            base_config=boost::gregorian::greg_durations_config
        ]
        c:\program files\boost\boost_1_39\boost\date_time\time.hpp(139): or       'boost::posix_time::ptime boost::date_time::base_time<T,time_system>::operator +(const boost::gregorian::date_duration &) const'
        with
        [
            T=boost::posix_time::ptime,
            time_system=boost::posix_time::posix_time_system
        ]
        c:\program files\boost\boost_1_39\boost\date_time\time.hpp(159): or       'boost::posix_time::ptime boost::date_time::base_time<T,time_system>::operator +(const boost::posix_time::time_duration &) const'
        with
        [
            T=boost::posix_time::ptime,
            time_system=boost::posix_time::posix_time_system
        ]
        while trying to match the argument list '(boost::system_time, const bool)'
        c:\program files\boost\boost_1_39\boost\thread\locks.hpp(353) : see reference to function template instantiation 'bool boost::detail::basic_timed_mutex::timed_lock<TimeDuration>(const Duration &)' being compiled
        with
        [
            TimeDuration=bool,
            Duration=bool
        ]
        c:\program files\boost\boost_1_39\boost\thread\locks.hpp(241) : see reference to function template instantiation 'bool boost::unique_lock<Mutex>::timed_lock<TimeDuration>(const TimeDuration &)' being compiled
        with
        [
            Mutex=boost::mutex,
            TimeDuration=bool
        ]
        d:\imagehawk\projects\virtualpc_archiveservice\ifl\src\archiveservice\archive.cpp(599) : see reference to function template instantiation 'boost::unique_lock<Mutex>::unique_lock<bool>(Mutex &,const TimeDuration &)' being compiled
        with
        [
            Mutex=boost::mutex,
            TimeDuration=bool
        ]

如果有帮助,这里是我的一些源代码:

void SpawnXMessageThread(bool bTakeLock=true) {
    boost::mutex::scoped_lock lock(m_mtx,bTakeLock);
    m_pxmessage= new XMessage();
    m_pThread = new boost::thread(boost::ref(*m_pxmessage));
}

我的想法是,我将根据 bool 值锁定 m_mtx。这样,如果我在调用 SpawnXMessageThread 之前锁定 m_mtx,我可以告诉 SpawnXMessageThread 不要锁定互斥锁(因为我已经锁定了它)。

顺便说一句,我对 boost 和线程有点陌生。

最佳答案

根据文档,您不能直接执行此操作。但是你可以这样做:

boost::unique_lock lck(m_mtx, boost::defer_lock);
if(bTakeLock) lck.lock();

关于c++ - 使用 boost::scoped_lock(Mutex &mx, bool initially_locked) 构造函数给我错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2450837/

相关文章:

c++ - 两个 Sprite 列表的碰撞 - SFML 2.0

c++ - boost lib 中的语句,(void)p;这是什么意思?

java - 在 Spring 支持的 java 独立应用程序中将 SwingUtilities.invokeLater 放在哪里?

c++ - 如何将使用 C++ 传递引用的函数转换为 C?

c++ - 高效的数据包类型/传输协议(protocol)

c++ - 将 std::async 与模板函数一起使用

c++ - 将 JSON 对象作为 Boost 中的键值对插入现有 JSON 对象中

c++ - 删除和修改 Boost MultiIndex 容器中的元素

c# - 多线程问题

c# - 是否可以重用 backgroundworker 对象?