dimanche 18 juillet 2021

why used double cheked lock pattern that std::memory_order_relaxed

I'm studying DCLP. I don't understand. why used load(std::memory_order_relaxed). Isn't this context the same as load(std::memory_order_seq_cst). because load(std::memory_order_relaxed) is not reordered in acquire/release model. Am I wrong? very confusing.. help me...

static MySingleton& getInstance()
{
    auto sin = instance.load(std::memory_order_acquire);
    if (!sin)
    {
        std::lock_guard<std::mutex> myLock(myMutex);
        sin = instance.load(std::memory_order_relaxed);
        if (!sin) {
            sin = new MySingleton();
            instance.store(sin, std::memory_order_release); // 1
        }
    }
    volatile int dummy{};

    return *instance;
}

Aucun commentaire:

Enregistrer un commentaire