mardi 29 août 2017

Why doesn't std::move on a std::unique_lock have any effect?

I have the following C++(11) code:

#include <mutex>

void unlock(std::unique_lock<std::mutex> && ulock)
{
}

int main(void)
{
    std::mutex m;
    std::unique_lock<std::mutex> ulock(m);

    unlock(std::move(ulock));

    if (ulock.mutex() == &m || ulock.owns_lock())
    {
        throw std::runtime_error("");
    }

    return 0;
}

What I can't figure out is why the mutex is still held after the return from unlock(). My expectation is that the std::move() causes the lock to go out of scope (and become unlocked by the destructor) upon return from the call to unlock(). At the very least, it seems like the std::move() should have caused ulock to become "unbound" from the mutex m.

What am I missing?

Aucun commentaire:

Enregistrer un commentaire