Boost logo

Boost :

Subject: Re: [boost] [smart ptr] Any interest in copy-on-write pointer for C++11?
From: Ralph Tandetzky (ralph.tandetzky_at_[hidden])
Date: 2013-02-08 13:17:31


On 02/08/2013 06:52 PM, Ralph Tandetzky wrote:
> On 02/08/2013 06:20 PM, Mathias Gaunard wrote:
>> On 08/02/13 17:50, Ralph Tandetzky wrote:
>>> It is as thread-safe as necessary. The reference counter is atomic.
>>> It's
>>> all well documented on github
>>> <https://github.com/ralphtandetzky/cow_ptr>. You can find all the
>>> thread-safety guarantees there. Thread-safety is documented at the
>>> bottom of the long comment preceeding the cow_ptr class.
>>
>> You should have just used std/boost::shared_ptr and added your COW
>> logic on top.
>> Your code reinvents the wheel, is quite inefficient (be it in terms
>> of memory layout, construction or assignment), and I'm not even sure
>> this is really thread-safe. Lockfree programming is tricky.
>> If I remember right, boost::shared_ptr required to make some copy
>> operations atomic as well, which requires a spinlock or 128-bit CAS.
>> Surely this also applies here.
>
> It wouldn't work to add the cow logic on top of a shared_ptr, when
> polymorphism is involved. Then an explicit clone() member function of
> the template type would be needed. The nice thing about the cow_ptr is
> that you can get this cloning facility non-intrusively.
>
> Concerning thread-safety: Calling member functions of cow_ptr is not
> atomic. However, two cow_ptrs that point to the same object can be
> used simultaneously to modify the object. In doubt both cow_ptrs will
> both make a copies of the pointed to object. In this sense it is safe
> to use cow_ptrs in multithreaded environments. However, my cow_ptr
> class is not thread-safe in the sense that you could write to one
> cow_ptr from several threads simultaneously. Implementing this might
> lead to a bad performance penalty.

Now that we're at it: What's wrong with the memory layout, construction
and assignment? How can it be made more effective? By the way, I have
implemented copy assignment and move assignment separately now, so the
code will be a little faster. Is that the kind of optimization you meant?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk