
Hi everyone, I specialized the boost::circular_buffer template class for concurrent access. But I have no idea about how the locking strategy must be for copy ctor, and also assignment operator. I inserted question marks at the end of the related lines. Thanks in advance... template <class T> class CircularBuffer { typedef boost::circular_buffer<T> internal_buffer; typedef boost::shared_mutex ReadWriteMutex; typedef boost::shared_lockboost::shared_mutex ReadLock; typedef boost::unique_lockboost::shared_mutex WriteLock; public: // Constructors explicit CircularBuffer(capacity_type capacity = 10) : m_capacity(capacity) , m_buffer(capacity) { } // copy ctor CircularBuffer(const CircularBuffer& other) { //WriteLock w_lock(rw_mutex); ??????????? m_capacity = other.m_capacity; m_buffer = other.m_buffer; } size_type size() const { ReadLock r_lock(rw_mutex); return m_buffer.size(); } void push_back(param_value_type item = value_type()) { WriteLock w_lock(rw_mutex); m_buffer.push_back(item); } // assignment operator CircularBuffer& operator= (const CircularBuffer& other) { //WriteLock w_lock(rw_mutex); ?????????? if (this != &other) { m_capacity = other.m_capacity; m_buffer = other.m_buffer; } return *this; } private:// member variables capacity_type m_capacity; internal_buffer m_buffer; // rw_mutex for internal buffer mutable ReadWriteMutex rw_mutex; };

AMDG Selçuk Giray Özdamar wrote:
You need a ReadLock on other. There is no need to lock the object being constructed.
// assignment operator
Use the swap trick. // assignment operator CircularBuffer& operator= (const CircularBuffer& other) { CircularBuffer temp(other); WriteLock w_lock(rw_mutex); using std::swap; swap(m_capacity, other.m_capacity); swap(m_buffer, other.m_buffer); return *this; } In Christ, Steven Watanabe
participants (3)
-
Ovanes Markarian
-
Selçuk Giray Özdamar
-
Steven Watanabe