Hi Y'all,
I'm looking at using an atomic reference count as sort of protection
for an object.
A thread using an object wants to be totally sure that a garbage
collector isn't going to kill it off, so it bumps the reference count
up while using it. Something like this...
struct obj {
int ref_count;
...
}
thread 1:
atomic_inc(&obj->ref_count);
do stuff to obj
atomic_dec(&obj->ref_count);
thread 2:
if(atomic_compare_swap(obj->ref_count, 0, -1))
nuke the object
Do I have this right? If I was using a critical section I'd trust
that the nuke-the-object code can't run at the same time as the
do-stuff-to-obj code.
Is treating ref_count atomically enough to ensure that I won't get a
nuke while processing the object?
I am assuming of course that no third thread randomly decrements the
reference count without incrementing (which would "unlock" the object
behind thread 1's back).
cheers
Ben