samedi 12 mai 2018

C++ lock mutex multiple times from same thread

I have background download thread that, if download is finished, call user specified callback. User can set same callback for different downloads.

In my callback code, I have this:

obj->lock();
obj->doSomething();
obj->unlock();

And obj is a global reference to Object.

class Object {
    std::mutex lck;

    void lock() { lck.lock(); }

    void unlock() { lck.unlock(); }

    void doSomething() {}

}

The above code is undefined behavior if called from the same thread. I have found recursive_mutex, but I am not sure if it is a correct solution.

Aucun commentaire:

Enregistrer un commentaire