Quantcast
Viewing latest article 2
Browse Latest Browse All 3

Answer by Alex Guteniev for Do I use atomic over shared memory correctly here

What I am worrying:

  • no placement new, just reinterpret cast + initial value set.
  • not a d-tor either.
  • why sizeof(uint16_t) is same as sizeof(std::atomic<uint16_t>) - is it always like this if is_always_lock_free ? Is std::atomic just afancy Assembly instruction?

These assumtions are likely to be true, but formally these are leading to UB.

To avoid running into UB, use C++20 atomic_ref. Share plain uint16_t and use atomic_ref to it in processes.

With atomic_ref it is still not formally guaranteed to work in an interprocess way (no interprocess in the standard C++ at all), but you will not be running into the mentioned UB, so no concerns about lifetime or incorrect aliasing.

Note that you have to keep is_always_lock_free assertion. Without it, atomic_ref would use a locking mechanism to provide atomic semantic, this mechanism is likely to be private to a program / process.


Viewing latest article 2
Browse Latest Browse All 3

Trending Articles