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

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

php - Cannot override Laravel Spark authentication with own implementation -

angular - DownloadURL return null in below code -