#include <wefts_referenced.h>
Inheritance diagram for Wefts::Referenced:
Public Methods | |
void | incRef () |
Increments the reference count of the varaible. | |
void | decRef () |
Decrements reference count. | |
Protected Methods | |
Referenced (int count=1, bool dispose=true) | |
Creates the referenced object with an initial count of users. | |
virtual | ~Referenced () |
The destructor may be called only by this class. | |
Private Attributes | |
int | m_count |
Count of users referencing the object. | |
bool | m_disposeable |
True if last reference should delete itself. |
Many times, complex multithread programs need to produce a dynamically allocated object that is then shared among many user threads. This object must then be available until the last thread needing it is done with it.
The most simple way to achieve this is to provide a threadsafe reference count that can be incremented or decremented. When the last user having a reference on the object releases it, the destructor is called.
This object is derived from a read/write mutex, so the users can access the mutex in read/only mode (which is most common when you are distributing an object to many user threads.
|
Creates the referenced object with an initial count of users. The count of current users can never be less than 1; it can be more if the creating process has a hint of the objects that are already waiting for this reference to be created. The disposeable parameter should be given and set to false if this object is not dinamically created, but is statically held somewhere int the creator's stack. In this case, the object won't be destroyed even when the reference count reaches 0, as it will be destroyed during owner's stack frame unwinding.
|
|
The destructor may be called only by this class.
|
|
Decrements reference count. If count reaches zero, the object is destroyed. Be careful not to cause race conditions so that you try to increment the reference of an object that is now being destroyed. |
|
Increments the reference count of the varaible. Be careful not to cause race conditions so that you try to increment the reference of an object that is now being destroyed. |
|
Count of users referencing the object.
|
|
True if last reference should delete itself.
|