<div dir="ltr">Wouldn&#39;t this proposal cover such cases - <a href="https://wg21.link/p1478">https://wg21.link/p1478</a>?<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, May 26, 2023 at 5:01 PM John Platts via Std-Proposals &lt;<a href="mailto:std-proposals@lists.isocpp.org">std-proposals@lists.isocpp.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">One feature that is missing from the C and C++ standards is to be able to do lock-free atomic accesses using a type that does not have the same size as the object (or subobject) being pointed to such as the following (at least in a portable way that does not involve undefined behavior):<br>
<br>
// ptr is aligned on a std::atomic_ref&lt;uint32_t&gt;::required_alignment boundary<br>
std::pair&lt;std::uint32_t, bool&gt; SomeFuncThatDoesU32AtomicAccess(std::uint8_t* ptr) {<br>
  std::uint32_t u32_bits = lock_free_atomic_load_32(ptr, std::memory_order_relaxed);<br>
  for(;;) {<br>
    if((u32_bits &amp; 0xFFFFu) == 0xFFFFu)<br>
      return std::make_pair(u32_bits, false);<br>
<br>
    const auto new_u32_bits = static_cast&lt;std::uint32_t&gt;(u32_bits + 1u);<br>
    if(lock_free_compare_exchange_weak_32(ptr, u32_bits, new_u32_bits,<br>
        std::memory_order_seq_cst, std::memory_order_relaxed))<br>
      return std::make_pair(u32_bits, true);<br>
  }<br>
}<br>
<br>
Note that memcpy cannot be used in the above code as memcpy can perform non-atomic accesses, as memcpy does an unconditional write, and as there is the possibility of data races on platforms that support threading.<br>
<br>
lock_free_atomic_load_32 is equivalent to the following, except that ptr can point to any valid memory location (including the case where any of the bytes accessed point to bytes of a volatile object and including the case where ptr points to read-only memory) where at least sizeof(std::uint32_t) bytes are readable and whose alignment is at least std::atomic_ref&lt;uint32_t&gt;::required_alignment):<br>
std::uint32_t lock_free_atomic_load_32(const volatile void* ptr,<br>
                                       std::memory_order order) {<br>
  std::atomic_ref&lt;std::uint32_t&gt; u32_atomic_ref(<br>
    *reinterpret_cast&lt;std::uint32_t*&gt;(const_cast&lt;void*&gt;(ptr)));<br>
  return u32_atomic_ref.load(order);<br>
}<br>
<br>
lock_free_compare_exchange_weak_32 is equivalent to the following, except that ptr can point to any valid memory location (including the case where any of the bytes accessed point to bytes of a volatile object) where at least sizeof(std::uint32_t) bytes are writable and whose alignment is at least std::atomic_ref&lt;uint32_t&gt;::required_alignment):<br>
bool lock_free_compare_exchange_weak_32(volatile void* ptr,<br>
                                        std::uint32_t&amp; expected,<br>
                                        std::uint32_t desired,<br>
                                        std::memory_order success,<br>
                                        std::memory_order failure) {<br>
  std::atomic_ref&lt;std::uint32_t&gt; u32_atomic_ref(<br>
    *reinterpret_cast&lt;std::uint32_t*&gt;(const_cast&lt;void*&gt;(ptr)));<br>
  return u32_atomic_ref.compare_exchange_weak(expected, desired, success, failure);<br>
}<br>
<br>
Adding lock-free atomic operations that can operate on any valid pointer to a suitably-aligned type such as lock_free_atomic_load_32, lock_free_atomic_store_32, lock_free_compare_exchange_weak_32, and lock_free_compare_exchange_strong_32 addresses issues that are not addressed by the std::memcpy and std::bit_cast operations.<br>
<br>
Having functions such as lock_free_atomic_load_32, lock_free_atomic_store_32, lock_free_compare_exchange_weak_32, and lock_free_compare_exchange_strong_32 would also allow C and C++ programmers to eliminate type punning in cases where std::memcpy cannot be used (due to the need to access memory that might be concurrently accessed in a lock-free atomic manner).<br>
-- <br>
Std-Proposals mailing list<br>
<a href="mailto:Std-Proposals@lists.isocpp.org" target="_blank">Std-Proposals@lists.isocpp.org</a><br>
<a href="https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals" rel="noreferrer" target="_blank">https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals</a><br>
</blockquote></div>

