Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: Multi threading and reference counting
    primarykey
    data
    text
    <p>Currently ive got some reference counted classes using the following:</p> <pre><code>class RefCounted { public: void IncRef() { ++refCnt; } void DecRef() { if(!--refCnt)delete this; } protected: RefCounted():refCnt(0){} private: unsigned refCnt; //not implemented RefCounted(RefCounted&amp;); RefCounted&amp; operator = (RefCounted&amp;}; }; </code></pre> <p>I also have a smart pointer class that handles reference counting , all though its not uniformly used (eg in one or two bits of performance critical code, where I minimised the number of IncRef and DecRef calls).</p> <pre><code>template&lt;class T&gt;class RefCountedPtr { public: RefCountedPtr(T *p) :p(p) { if(p)p-&gt;IncRef(); } ~RefCountedPtr() { if(p)p-&gt;DecRef(); } RefCountedPtr&lt;T&gt;&amp; operator = (T *newP) { if(newP)newP-&gt;IncRef(); if(p) p -&gt;DecRef(); p = newP; return *this; } RefCountedPtr&lt;T&gt;&amp; operator = (RefCountedPtr&lt;T&gt; &amp;newP) { if(newP.p)newP.p-&gt;IncRef(); if(p) p -&gt;DecRef(); p = newP.p; return *this; } T&amp; operator *() { return *p; } T* operator -&gt;() { return p; } //comparison operators etc and some const versions of the above... private: T *p; }; </code></pre> <p>For the general use of the classes themselves I plan to use a reader/writer locking system, however I dont really want to have to get a writer lock for every single IncRef and DecRef call.</p> <p>I also just thought of a scenario where the pointer may be invalidated just before the IncRef call, consider:</p> <pre><code>class Texture : public RefCounted { public: //...various operations... private: Texture(const std::string &amp;file) { //...load texture from file... TexPool.insert(this); } virtual ~Texture() { TexPool.erase(this); } freind CreateTextureFromFile; }; Texture *CreateTexture(const std::string &amp;file) { TexPoolIterator i = TexPool.find(file); if(i != TexPool.end())return *i; else return new Texture(file); } </code></pre> <pre> ThreadA ThreadB t = CreateTexture("ball.png"); t->IncRef(); ...use t... t2 = CreateTexture("ball.png");//returns *t ... thread suspended... t->DecRef();//deletes t ... ... t2->IncRef();//ERROR </pre> <p>So I guess I need to change the ref counting model entirely, the reason I added a ref after the return in the design was to support things like the following:</p> <pre><code>MyObj-&gt;GetSomething()-&gt;GetSomethingElse()-&gt;DoSomething(); </code></pre> <p>rather than having to:</p> <pre><code>SomeObject a = MyObj-&gt;GetSomething(); AnotherObject *b = a-&gt;GetSomethingElse(); b-&gt;DoSomething(); b-&gt;DecRef(); a-&gt;DecRef(); </code></pre> <p>Is there a clean way for fast reference counting in c++ in a multi threaded environment?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload