samedi 27 février 2016

Is the execution of a c++ 11 atomic object also atomic ?

I have a object and all its function should be executed in sequential order. I know it is possible to do that with a mutex like

#include <mutex> 

class myClass {
private:
    std::mutex mtx;
public:
    void exampleMethod();
};

void myClass::exampleMethod() {
    std::unique_lock<std::mutex> lck (mtx); // lock mutex until end of scope

    // do some stuff
}

but with this technique a deadlock occurs after calling an other mutex locked method within exampleMethod.

so i'm searching for a better resolution. the default std::atomic access is sequentially consistent, so its not possible to read an write to this object at the same time, but now when i access my object and call a method, is the whole function call also atomic or more something like

object* obj = atomicObj.load(); // read atomic
obj.doSomething(); // call method from non atomic object;

if yes is there a better way than locking the most functions with a mutex ?

Aucun commentaire:

Enregistrer un commentaire