Thread safety

Thread-safe reference counting

Increment refcount: atomic increment of memory location. Most processors provide special support for this, so no lock is needed around the refcount. In fact, this typically how locks/semaphores are implemented. Compilers can provide an atomic increment builtin.

Free reference: atomic decrement-and-test. If the refcount was initially 1, we own the object (no other threads refer to the object) and are responsible for freeing it.

Acquiring reference from a field of a shared object: This is trickier than acquiring a reference as an argument or as the result of a function call. The shared object should be locked while the reference count of the object from the field is incremented.

ThreadSafety (last edited 2007-05-17 22:35:24 by MarkSeaborn)