I am wondering how can std::atomic_ref
be implemented efficiently (one std::mutex
per object) for non-atomic objects as the following property seems rather hard to enforce:
Atomic operations applied to an object through an atomic_ref are atomic with respect to atomic operations applied through any other atomic_ref referencing the same object.
In particular, the following code:
void set(std::vector<Big> &objs, size_t i, const Big &val) {
std::atomic_ref RefI{objs[i]};
RefI.store(val);
}
Seems quite difficult to implement as the std::atomic_ref
would need to somehow pick every time the same std::mutex
(unless it is a big master lock shared by all objects of the same type).
Am I missing something? Or each object is responsible to implement std::atomic_ref
and therefore either be atomic or carry a std::mutex
?