#include <util.h>

Public Member Functions | |
| unsigned | reference () |
| Increment the reference count. | |
| unsigned | dereference () |
| Decrement the reference count. | |
| unsigned | getReferenceCount () const |
It keeps track of reference counts. It will automatically delete the object when its reference count is decremented to zero.
This class is especially useful if you want to throw a newly allocated pebble into a queue and have it auto-garbage-collected after it has run. The pebble class inherits referable, and it also includes the pebble::dereferenceAfterRun method, which, if called, sets the pebble up so that when it is added to the exeque, its reference count is incremented, and then after its run, its reference count is decremented. If its reference count starts from zero before it is added to the exeque (as it would be if its just been allocated) then when it has been run, its reference count will become zero, and it will be automatically deleted.
It contains a reference ound integer, and a semaphore which is used to lock accesses to the reference count. The reference count is not a semaphore, though this would have been the simplest way to implement a counter. Its not a semaphore because when it goes to zero, we need to synchronously determine that now it is time to delete the object. This must be done inside of a semaphore lock, otherwise race conditions can happen. Hence, it has to do two semaphore operations for each increment or decrement, but there is no avoiding this.
| unsigned karoo::referable::dereference | ( | ) | [inline] |
Decrement the reference count.
When the reference count goes from 1 to zero, the object will be deleted. Therefore you should not call dereference() on an object that was not allocated with new.
| unsigned karoo::referable::reference | ( | ) | [inline] |
Increment the reference count.
1.5.8