[Development] atomic reference counting implementation

Philippe philwave at gmail.com
Wed Aug 7 18:36:38 CEST 2019


I recently found that in Qt,  reference counting to guard a resource, is using
ref() / deref()

But ref() is using memory_order_seq_cst
while memory_order_relaxed, should be sufficient

What is important is to guarantee that destruction is not subject to a race condition, to prevent double
destruction. Hence deref() with memory_order_seq_cst is enough to guarantee
that.

It does not matter how much the counter increase, but what is important
is to control how it is decreased. Hence deref(with memory_order_seq_cst)
is just enough.

I have verified the implementaiton of reference counting for shared_ptr
in clang, and it does what I describe above
(it even just use memory_order_acq_rel to decrement, and not memory_order_seq_cst)

https://github.com/llvm-mirror/libcxx/blob/master/include/memory#L3344

Is there a reason why Qt is not optimized in the same way? (since ref() is
used a lot, and atomic operations are a bit expensive).

Is there a requirement at some stage that the reference counter must be
ordered for increments?

Philippe




More information about the Development mailing list