C++ Logo

std-discussion

Advanced search

LWG2445 - what about C++11-17?

From: Marc Mutz <marc.mutz_at_[hidden]>
Date: Fri, 12 Jul 2019 19:10:05 +0200
Hi,

So, LWG2445 (wg21.link/lwg2445) is resolved by merging P0418R2
(wg21.link/p0418r2) for C++20. But what about C++11-17? It was a DR, so
what's the resolution for older standards?

Can we write a.compare_exchange_strong(expected, desired,
std::memory_order_release, std::memory_order_acquire), or not?

Use case: lazy initialization of a pool of elements:

     std::atomic<T*> pool[N];
     T *get(size_t n) {
         T *t = pool[N].load(std::memory_order_acquire);
         if (!t) {
             auto tmp = new T;
             if (!pool[n].compare_exchange_strong(t, tmp,
                      std::memory_order_release, // don't need acq, as we
created the data in our thread
                      std::memory_order_acquire)) // data created in
another thread, need acq
                 delete tmp;
         }
         return t;
     }

Valid C++11, 14? 17? Or _just_ 20?

Thanks,
Marc

Received on 2019-07-12 12:12:02