lundi 25 mai 2015

thread safe object state manipulation in c++11

I am trying to do the following in a multi-threaded environment (it does not implement the solution correctly at the moment but you can get the intent)

struct object {
    object() : numReaders(0) {}
    void try_read() {
       numReaders++;
       if(valid) {
           // do something
       }
       numReaders--;
    }

    void destroy() {
       if(numReaders == 0) {
           // <- is there is a reader here we have a problem
           valid = 0;
       }
    }
    std::atomic<int> numReaders;
};

this sure looks like a std::shared_mutex or a reader/writer lock problem. Is this particular solvable in the context of c++11 in a clean and short way (without copying implementations from c++14 or smth) and without using third party libraries?

Aucun commentaire:

Enregistrer un commentaire