Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-06-09 12:14:16


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net