1

Are the hat pointers (ref pointers) atomic in C++/CX? And if no, what are the equivalents of std::atomic_load and std::atomic_store for them?

And how to make the following code thread-safe?

public ref class A sealed { ... };

A ^ p_a = nullptr;
A ^ p_a1 = ref new A();

//thread 1
p_a = p_a1;

//thread 2
p_saved = p_a;
if (p_saved != nullptr)
{
    p_saved->Func();
}

For example,

std::atomic<A ^> p_a; 

does not compile.

Alexey Starinsky
  • 3,699
  • 3
  • 21
  • 57
  • The handle-to-object operator ^ is known as a "hat" and is fundamentally a C++ smart pointer. – Severin Pappadeux May 15 '18 at 12:16
  • Thank you for the information, but I am worrying a bit that the pointer itself may be not atomic like int for example, see https://developernote.com/2017/08/cpp-why-int-is-not-atomic/ – Alexey Starinsky May 16 '18 at 10:52
  • That example explicitly writes to non-aligned memory; it's trying very hard to be broken to prove a point. [The read / write of a naturally-aligned pointer should be atomic](https://msdn.microsoft.com/en-us/library/windows/desktop/ee418650(v=vs.85).aspx), so you won't get a half-written pointer. But you can't guarantee which happens first, so either `p_saved` will be valid (and the object will have a refcount of 3) or `p_saved` will be `nullptr`. – Peter Torr - MSFT May 20 '18 at 19:25
  • Interesting idea, I asked a new question about natural alignment: https://stackoverflow.com/questions/50468186/natural-alignment-volatile-atomic-in-c11 – Alexey Starinsky May 22 '18 at 12:58

0 Answers0