I have a class of objects in a multithreaded application where each thread can mark an object for deletion, then a central garbage collector thread actually deletes the object. The threads communicate via member methods that access an internal bool:
class MyObjects {
...
bool shouldBeDeleted() const
{
return m_Delete;
}
void
markForDelete()
{
m_Delete = true;
}
...
std::atomic< bool > m_IsObsolete;
}
The bool has been made an atomic by someone else in the past because Thread Sanitizer kept complaining. However, perf suggests now that there is a processing overhead during the internal atomic load:
│ ↓ cbz x0, 3f4
│ _ZNKSt13__atomic_baseIbE4loadESt12memory_order():
│ {
│ memory_order __b = __m & __memory_order_mask;
│ __glibcxx_assert(__b != memory_order_release);
│ __glibcxx_assert(__b != memory_order_acq_rel);
│
│ return __atomic_load_n(&_M_i, __m);
│ add x0, x0, #0x40
86,96 │ ldarb w0, [x0]
Target platform is GCC, Aarch64 and Yocto Linux.
Now my questions are as follows:
Is atomic really needed in this case? The transition of the bool is one way (from false to true) with no way back while the object lives, so an inconsistency would merely mean that the object is deleted a little later, right?
Is there an alternative to
std::atomic<bool>
that will silence Thread Sanitizer but is computationally cheaper thanstd::atomic<bool>
?