2 ms·
With ARC sharing objects across threads becomes trickier. First of all, in the general case you'll have to do atomic increments and decrements which tend to be
by sanjoy_das 11y ago
With ARC sharing objects across threads becomes trickier. First of
all, in the general case you'll have to do atomic increments and
decrements which tend to be fairly expensive, and they'll be sitting
right in the middle your core application logic. Secondly, if you're
sharing objects amongst threads, you cannot simply do:
Thread1:
Obj = Heap->field;
Heap->field = null;
// reduced a reference so:
if (AtomicDecrement(Obj->refcount) == 0) {
free(Obj);
}
Since you'll be racing with
Thread2:
Obj = Heap->field;
// stalls, and Thread1 deletes Obj
AtomicIncrement(Obj->refcount)
The only satisfactory solution to this that I'm aware of is to use
hazard pointers, and that is a fairly complex bit of logic. Maybe
there's a better solution to this, but I've not come across one.
- deleted 11y ago[deleted]