Suppose i have an atomic pointer:
std::atomic<void*> mItems;
and in a function when one thread need to access that, it first check it, and if it is null, thread will allocate memory for it:
void* lItems = std::atomic_load_explicit(&mItems, memory_order_relaxed);
if(lItems == nullptr)
{
    void* lAllocation = malloc(...);
    if(!std::atomic_compare_exchange_strong_explicit(
        &mItems, 
        &lItems, 
        lAllocation, 
        memory_order_relaxed, 
        memory_order_relaxed))
    {
        free(lAllocation);
    }
}
    ...
But if N thread run this method concurrency and see mItems equal to null then all of them will allocate memory and N - 1 of them will free agian.
How i can write similar method with better approach.
 
     
    