mercredi 21 décembre 2016

Initialization of std::atomic

This question already has an answer here:

I want to properly initialize std::atomic of user defined type:

struct SimpleType
{
    SimpleType* next;
    int count;
};

class A
{
    std::atomic<SimpleType> mem;

public:
    A()
    {
        SimpleType newST;
        newST.next = nullptr;
        newST.count = 0;

        SimpleType oldST = mem.load();
        while (!mem.compare_exchange_weak(oldST, newST));
    }
};

Is there a way not to write this awful code in constructor of A? What if I want to have multiple atomic members?

If I define constructor in SimpleType, there will be a compiler error: Error C2280 'std::atomic::atomic(void) noexcept': attempting to reference a deleted function

Aucun commentaire:

Enregistrer un commentaire