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:
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.
// copy ctor CircularBuffer(const CircularBuffer& other) { //WriteLock w_lock(rw_mutex); ??????????? m_capacity = other.m_capacity; m_buffer = other.m_buffer; }
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
Steven,
please see my question below.
On Mon, Jun 9, 2008 at 6:14 PM, Steven Watanabe
AMDG [...]
// assignment operator CircularBuffer& operator= (const CircularBuffer& other) { CircularBuffer temp(other);
why do you need this temp, if it is not used further?
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 http://lists.boost.org/mailman/listinfo.cgi/boost-users
With Kind Regards, Ovanes
AMDG Ovanes Markarian wrote:
// assignment operator CircularBuffer& operator= (const CircularBuffer& other) { CircularBuffer temp(other);
why do you need this temp, if it is not used further?
WriteLock w_lock(rw_mutex);
using std::swap; swap(m_capacity, other.m_capacity); swap(m_buffer, other.m_buffer);
Whoops. swap(m_capacity, temp.m_capacity); swap(m_buffer, temp.m_buffer); In Christ, Steven Watanabe
participants (3)
-
Ovanes Markarian
-
Selçuk Giray Özdamar
-
Steven Watanabe