mercredi 26 octobre 2016

Vector of class containing atomic varialbles

I was attempting to write a threadpool implementation and I came across a problem dealing with storage of atomic values. I started making this class:

class ThreadObj
{
public:
    ...
    std::atomic<bool> isWaiting;
};

std::vector<ThreadObj> threadPool;

Because atomic variables are not able to be copied or moved, I was unable to put it into the vector, even using emplace_back(...). My solution to the problem was to use this instead:

    std::unique_ptr <std::atomic<bool> > isWaiting = {std::unique_ptr <std::atomic<bool> > (new std::atomic<bool> (false))};

Is this solution a reasonable one for storing atomic values? Other posts suggested making a wrapper for atomic variables to make it copy and move assignable. Would that be a better solution?

Aucun commentaire:

Enregistrer un commentaire