
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.