galdar496 / QiGameEngine

Realtime Game Engine designed with C++ and OpenGL.
http://qiengine.blogspot.com/
1 stars 0 forks source link

Create a base class for reference counting any objects that need to be reference counted #34

Closed galdar496 closed 9 years ago

galdar496 commented 9 years ago

Store a reference count internally and have functions to increment/decrement the reference count.

Functions:

Use an atomic and compare_and_exchange in order to perform proper threadsafety for all reference counted objects. Logic:

uint32 oldCount;
uint32 newCount;
do
{
    oldCount = m_count;
    newCount = oldCount - 1;
} while (compare_and_exchange(&m_count, oldCount, newCount);

if (newCount == 0)
{
    DeleteThisObject();
}
galdar496 commented 9 years ago

f3da0d8f530e6e38de37bab6250cd303b0a3d2a2