代码之家  ›  专栏  ›  技术社区  ›  hookenz

如何将复制构造函数和赋值运算符添加到此类?

  •  1
  • hookenz  · 技术社区  · 14 年前

    我在向此类中添加复制构造函数时遇到问题: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html

    我需要添加它,以便可以在stl向量容器中添加并发队列。

    concurrent_queue(concurrent_queue<Data> const& rhs):
        the_queue(rhs.the_queue),
        the_mutex(rhs.the_mutex),
        the_condition_variable(rhs.the_condition_variable)
    {
    }
    
    concurrent_queue<Data>& operator = (concurrent_queue<Data> const& rhs)
    {
        if (this == &rhs) return *this; // check for self assignment
    
        the_queue = rhs.the_queue;
        the_mutex(rhs.the_mutex);
        the_condition_variable(rhs.the_condition_variable);
    }
    

    我得到的错误如下:

    concurrentqueue.h: In copy constructor ‘concurrent_queue<Data>::concurrent_queue(const concurrent_queue<Data>&) [with Data = Packet*]’:
    /usr/include/c++/4.4/bits/stl_construct.h:74:   instantiated from ‘void std::_Construct(_T1*, const _T2&) [with _T1 = concurrent_queue<Packet*>, _T2 = concurrent_queue<Packet*>]’
    /usr/include/c++/4.4/bits/stl_uninitialized.h:187:   instantiated from ‘static void std::__uninitialized_fill_n<<anonymous> >::uninitialized_fill_n(_ForwardIterator, _Size, const _Tp&) [with _ForwardIterator = concurrent_queue<Packet*>*, _Size = long unsigned int, _Tp = concurrent_queue<Packet*>, bool <anonymous> = false]’
    /usr/include/c++/4.4/bits/stl_uninitialized.h:223:   instantiated from ‘void std::uninitialized_fill_n(_ForwardIterator, _Size, const _Tp&) [with _ForwardIterator = concurrent_queue<Packet*>*, _Size = long unsigned int, _Tp = concurrent_queue<Packet*>]’
    /usr/include/c++/4.4/bits/stl_uninitialized.h:318:   instantiated from ‘void std::__uninitialized_fill_n_a(_ForwardIterator, _Size, const _Tp&, std::allocator<_Tp2>&) [with _ForwardIterator = concurrent_queue<Packet*>*, _Size = long unsigned int, _Tp = concurrent_queue<Packet*>, _Tp2 = concurrent_queue<Packet*>]’
    /usr/include/c++/4.4/bits/stl_vector.h:1035:   instantiated from ‘void std::vector<_Tp, _Alloc>::_M_fill_initialize(size_t, const _Tp&) [with _Tp = concurrent_queue<Packet*>, _Alloc = std::allocator<concurrent_queue<Packet*> >]’
    /usr/include/c++/4.4/bits/stl_vector.h:230:   instantiated from ‘std::vector<_Tp, _Alloc>::vector(size_t, const _Tp&, const _Alloc&) [with _Tp = concurrent_queue<Packet*>, _Alloc = std::allocator<concurrent_queue<Packet*> >]’
    test.cpp:18:   instantiated from here
    concurrentqueue.h:24: error: no match for call to ‘(boost::mutex) (boost::mutex&)’
    

    编辑: boost mutex似乎继承了不可复制的条件变量,我认为是相同的。

    2 回复  |  直到 14 年前
        1
  •  2
  •   Georg Fritzsche    14 年前

    两者 boost::mutex boost::condition_variable 只有默认构造函数。除此之外,您不希望复制它们(可能已锁定)的状态-只需使用默认构造函数。

    the_queue 直接因为这不是线程安全的。

        2
  •  1
  •   Community rohancragg    7 年前

    阿法克, boost::mutex boost::condition 是不可复制的(而且无论如何复制互斥锁是没有意义的)。使用复制构造函数中的默认构造函数构造它们,如下所示:

    concurrent_queue(concurrent_queue<Data> const& rhs):
        the_queue(rhs.the_queue),
        the_mutex(),
        the_condition_variable()
    {
    }
    

    注意有 problems 使复制构造函数线程安全。如果你没有在线程之间复制对象,这应该不是问题。遵循你的想法 previous question concurrent_queue 对象是预先分配的,这不应该是问题。