3

I try to understand how ::operator delete sync with other atomic operation. Is any atomic::write --SC-order--> ::operator delete in the same thread?

atomic<void*> A;
T1: old = A.read(relaxed); A.write(new_value,relaxed); delete(old);
T2: v1=A.read(relaxed);

Is the order possible:

T1: delete(old);
T2: v1=A.read(relaxed)
T1: A.write(new_value, relaxed);

Can T1:write(new_value, relaxed) be after T1:delete(old) for T2 in theory?

10
  • In your example, it doesn't even matter. T2: v1=read(A, relaxed); can certainly happen before A.write(new,relaxed); delete(old);, leaving T2 with a dangling pointer. No complicated reordering necessary, may happen even with full sequential consistency. Commented Sep 20 at 13:37
  • @IgorTandetnik I know that this situation is also possible, it's just an example. I'm interested in this particular scenario that I mentioned. Commented Sep 20 at 13:49
  • The C++ memory model is not defined in terms of reorderings, but in terms of what values may or may not be observed. May T2 observe the old value? Yes it may. The question you pose cannot be answered - or even asked - within the terminology of the C++ standard. Can you show an example where reordering you are concerned about would lead to observable difference in behavior? Commented Sep 20 at 13:54
  • @IgorTandetnik If the pointer is deleted and the new value is not visible to T2, then T2 will read the deleted pointer and access to the pointer is UB Commented Sep 20 at 14:04
  • 2
    The answer to your question is what Igor said. It's a mistake to think that there's a single global timeline that you can order things on. Each thread has its own timeline, each variable has its own, and there's the seq-cst timeline. You need to specify what timeline you're asking about. In the timeline of T1, the delete happens after the write just because it's sequenced after it. Other threads may or may not observe those in different order. Commented Sep 20 at 14:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.