lundi 28 novembre 2016

Is std::lock_guard

I need to assert on unique_lock::owns_lock() in a working method, but I dont' want to pass the unique_lock as an argument but use it as a static variable. But that means I cannot use the unique_lock for its RAII behavior. Would it be fair to have code like:

namespace
{
    std::mutex gMtx;
    std::unique_lock<std::mutex> gLock(gMtx, std::defer_lock);
}

void foo()
{
    assert(gLock.owns_lock());
    // ...
}

void bar()
{
    std::lock_guard<std::unique_lock<std::mutex>> lock(gLock);
    //...
    foo();
    //...
}

Thoughts?

Aucun commentaire:

Enregistrer un commentaire