簡體   English   中英

如何向此類添加復制構造函數和賦值運算符?

[英]How do I add a copy constructor and assignment operator to this class?

我在將復制構造函數添加到此類時遇到了麻煩: http : //www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html

我需要添加它,以便可以在stl向量容器中添加concurrent_queue。 在下面嘗試過,幾乎可以編譯。 實際上,如果我從復制構造函數中刪除the_mutex和the_condition_variable,則會對其進行編譯。 當我添加回它沒有。 賦值運算符似乎可以編譯。

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::mutexboost::condition_variable只有默認構造函數。 除此之外,您不希望復制其(潛在鎖定)狀態-只需使用默認構造函數即可。

另外,您不應該直接復制the_queue ,因為這不是線程安全的。

AFAIK, boost::mutexboost::condition不可復制(無論如何復制互斥鎖都沒有意義)。 使用復制構造函數中的默認構造函數來構造它們,如下所示:

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

請注意,使復制構造函數線程安全存在問題 但是,如果您不是在線程之間復制對象,那應該不是問題。 遵循上一個問題中的想法,在該想法中,所有concurrent_queue對象都已預先分配,這應該不成問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM