Store a reference count internally and have functions to increment/decrement the reference count.
Functions:
AddReference()
RemoveReference()
GetReferenceCount()
DeleteThisObject()
\ Make this function virtual so that objects can customize the deletion behavior. For example, placing the objects on a list for later deletion.
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();
}
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: