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