#include <wefts_referenced.h>
Inheritance diagram for Wefts::Referenced:
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.
Public Member Functions | |
void | incRef () |
Increments the reference count of the varaible. | |
void | decRef () |
Decrements reference count. | |
int | references () |
Returns the reference count of an object. | |
void | incRefUnlocked () |
Increments the reference of the object without using the write locking. | |
void | setRefUnlocked (int count) |
Set Reference count to given amount. | |
Protected Member Functions | |
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. |
|
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. |
|
Increments the reference of the object without using the write locking. This is meant to be used ONLY in code blocks where this object is already write locked. See the references() method for an example. |
|
Returns the reference count of an object. To be sure that the count of this object is still valid after this call, the caller must surround this call with a lock. Normally a read lock will be enough, but if the caller is going to reference this object more, with an incRefUnlocked, then a call inside a writeLock() is needed:
|
|
Set Reference count to given amount. This is meant to be used ONLY in code blocks where this object is already write locked. See the references() method for an example.
|
|
Count of users referencing the object.
|
|
True if last reference should delete itself.
|