【问题标题】:boost unable to move a scoped_lock with gccboost 无法使用 gcc 移动 scoped_lock
【发布时间】:2012-09-17 17:29:09
【问题描述】:

以下是在 VS2010 (Express) 下编译,但不是 gcc (这里是 4.6.2)。

Lockable.h:

#include <boost/thread/mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>

template<typename T>
class LockedProxy : boost::noncopyable
{
public:
    inline LockedProxy(boost::mutex & m, T * obj)
        :lock(m),
        t(obj)
    {}
    inline LockedProxy(const LockedProxy && other)
        :lock(std::move(other.lock)),
        t(std::move(other.t))
    {}

    inline       T * operator->()       { return t; }
    inline const T * operator->() const { return t; }

    inline const T & operator*() const { return *t; }
    inline       T & operator*()       { return *t; }

private:
    boost::interprocess::scoped_lock<boost::mutex> lock;
    T * t;
};


template<typename T>
class Lockable
{
public:

    // Convenience typefed for subclasses to use
    typedef T LockableObjectType;

    inline Lockable(const T & t)
        :lockableObject(t)
    {}

    inline LockedProxy<LockableObjectType> GetLockedProxy() {
        return LockedProxy<LockableObjectType>(mutex, &lockableObject);
    }

protected:
    LockableObjectType lockableObject;
    boost::mutex mutex;
};

ma​​in.cpp:

#include <iostream>
#include <string.h>
#include "Lockable.h"    

void f(Lockable<std::string> & str)
{
    auto proxy = str.GetLockedProxy();

    *proxy = "aa";
    proxy->append("bb");

    std::cout << "str = " << *proxy << std::endl;
}

void g(Lockable<int> & i)
{
    { // reduce lock's lifespan
        auto proxy = i.GetLockedProxy();
        *proxy = 321;
    }

    // relock, lock lives for the statement
    std::cout << "i = " << *i.GetLockedProxy() << std::endl;
}

int main()
{
    Lockable<std::string> str("abc");
    //Can't use str here, it is not locked
    f(str);

    Lockable<int> i(123);
    g(i);

    return 0;
}

错误:

In file included from main.cpp:3:0:
C:/Users/DrGibbs/Documents/code/boost_1_46_0/boost/interprocess/sync/scoped_lock.hpp: In constructor 'LockedProxy<T>::LockedProxy(const LockedProxy<T>&&) [with T = std::basic_string<char>, LockedProxy<T> = LockedProxy<std::basic_string<char> >]':main.cpp:7:37:   instantiated from here
C:/Users/DrGibbs/Documents/code/boost_1_46_0/boost/interprocess/sync/scoped_lock.hpp:56:4: error: 'boost::interprocess::scoped_lock<Mutex>::scoped_lock(const boost::interprocess::scoped_lock<Mutex>&)[with Mutex = boost::mutex, boost::interprocess::scoped_lock<Mutex> = boost::interprocess::scoped_lock<boost::mutex>]' is privateLockable.h:14:29: error: within this context
C:/Users/DrGibbs/Documents/code/boost_1_46_0/boost/interprocess/sync/scoped_lock.hpp: In constructor 'LockedProxy<T>::LockedProxy(const LockedProxy<T>&&) [with T = int, LockedProxy<T> = LockedProxy<int>]':
main.cpp:18:36:   instantiated from here
C:/Users/DrGibbs/Documents/code/boost_1_46_0/boost/interprocess/sync/scoped_lock.hpp:56:4: error: 'boost::interprocess::scoped_lock<Mutex>::scoped_lock(const boost::interprocess::scoped_lock<Mutex>&)[with Mutex = boost::mutex, boost::interprocess::scoped_lock<Mutex> = boost::interprocess::scoped_lock<boost::mutex>]' is private
Lockable.h:14:29: error: within this context

据我了解,在LockedProxy 的移动构造函数中,scoped_lock 不是移动而是复制构造的,这确实不应该工作。 std::move 不应该保证它的移动构造吗?我错过了什么?

【问题讨论】:

    标签: c++ boost c++11 move-semantics scoped-lock


    【解决方案1】:

    你的移动构造函数声明了它的参数const

    inline LockedProxy(const LockedProxy && other)
    

    它应该被声明为非常量:

    inline LockedProxy(LockedProxy && other)
    

    您的std::move(other.lock)other.lock 作为一个常量引用,因此返回一个常量右值引用const boost::interprocess::scoped_lock&lt;boost::mutex&gt; &amp;&amp;,它不能传递给boost::interprocess::scoped_lock&lt;boost::mutex&gt; 的移动构造函数。

    另见C++0x const RValue reference as function parameter,它解释了 const 右值引用几乎完全没用。

    【讨论】:

      猜你喜欢
      • 2011-01-17
      • 1970-01-01
      • 2011-07-15
      • 2011-02-22
      • 1970-01-01
      • 2014-07-28
      • 2014-08-06
      • 2015-02-23
      • 1970-01-01
      相关资源
      最近更新 更多