[gclist] Multi threaded reference counting
Sun, 19 Jan 1997 00:18:13 -0700
In the GC-FAQ on the web there is this quote :
>In a multi-threaded system, if threads are preemptively scheduled,
reference counting requires
>additional care to ensure that the updates to reference counts are atomic.
This is straightforward
>using hardware primitives such as fetch-and-add, compare-and-swap, or
>load-linked/store-conditional, but it is definitely necessary to pay
attention to this detail.
If it is straightforward I can't figure it out. I beleive that there
is a race condition in simple reference counting that cannot be
got around with the usual atomic primitives. If someone does
have a solution I'd like to see it.
The race condition from my previous post repeated here :
say array X contains the only reference to object Y and thread A
stores NIL into X while thread B pushes X onto its stack.
It is possible for Y's refcount to become zero due to thread A
as thread B tries to increment it.
Say the code executes in the following sequence:
/* load contents of X to a register. */
obj = X;
thread A :
/* store NIL in object, get previous contents */
prevobj = fetch_and_store(&X, NIL);
/* decrement refcount of previous contents */
/* free it */
if (refcount <= 0)
thread B :
obj->refcount++; /* crash! object has been freed by thread A. */
--- james mccartney email@example.com firstname.lastname@example.org
If you have a PowerMac check out SuperCollider, a real time synth program: