c++ - Thread safe makeUnique function for copy-on-write objects -
i'd create thread-safe copy-on-write object. i'd have function makeunique, checks reference counter: if 1, nothing. if not 1, clones data. basic non-thread-safe implementation this:
class cow { private: struct data { int refcount; // various other members }; data *m_data; public: void makeunique() { if (m_data->refcount!=1) { data *newdata = m_data->clone(); // returns cloned data, refcount==1 decref(m_data); m_data = newdata; } } }; which efficient way make thread-safe? targeting x86 , armv7+.
basically, think need atomic "compare-and-decrement" operation, can done compare-and-swap in loop. there more performant way (considering target platforms)?
Comments
Post a Comment