c++multithreadingunique-lock

Why std::lock() causes the endless loop when works with my own unique_lock object?


I am trying to implement the analog of unique_lock (it's just studying task, I understand that standard library implementation works perfectly).

I've already written all the methods that I need and now I am trying to test my code on the example from https://en.cppreference.com/w/cpp/thread/unique_lock/unique_lock.

When it comes to std::lock(lk_b, lk_c); the infinite loop starts.

I did some cout's to understand where the program loses control and the result is the following: lock -> try -> unlock -> lock -> try -> unlock.

Here is partial unique_lock implementation (I included only those methods, which are used in the problem part of the example).

template<typename Mutex>
class my_unique_lock {
    Mutex *lockable;
    bool is_acquired;
public:
    explicit my_unique_lock(Mutex& m): lockable{&m}, is_acquired{true}{
        lockable->lock();
        //std::cout << "constructor my_unique_lock(Mutex& m)" << std::endl;
    }

    my_unique_lock(Mutex& m, std::defer_lock_t t): lockable{&m}, is_acquired{false}{
        std::cout << "constructor my_unique_lock(Mutex& m, std::defer_lock_t t)" << std::endl;
    }

    bool try_lock(){
        std::cout << "try_lock" << std::endl;
        if(lockable == nullptr)
            throw std::system_error();
        is_acquired = mutex()->try_lock();
        return is_acquired;
    }

    void lock(){
        std::cout << "lock" << std::endl;
        if(lockable == nullptr || owns_lock())
            throw std::system_error();
        mutex()->lock();
        is_acquired = true;
    }

    void unlock(){
        //std::cout << "unlock" << std::endl;
        if(lockable == nullptr || !owns_lock())
            throw std::system_error();
        mutex()->unlock();
        is_acquired = false;
        std::cout << "unlocked" << std::endl;
    }

    Mutex *mutex() const noexcept {
        //std::cout << "*mutex()" << std::endl;
        return lockable;
    }

    bool owns_lock() const noexcept {
        //std::cout << "owns_lock()" << std::endl;
        return lockable != nullptr && is_acquired;
    }

    ~my_unique_lock(){
        //std::cout << "destructor" << std::endl;
        if(mutex() != nullptr && owns_lock()){
            mutex()->unlock();
            is_acquired = false;
        }
    }
};

And here is the example.


void update(std::mutex &m_a, std::mutex &m_b, std::mutex &m_c, int &a, int &b, int &c)
{
    {   
        my_unique_lock<std::mutex> lk(m_a);
        a++;
    }

    { 
        my_unique_lock<std::mutex> lk_b(m_b, std::defer_lock);
        my_unique_lock<std::mutex> lk_c(m_c, std::defer_lock);
        std::lock(lk_b, lk_c);
        b = std::exchange(c, b + c);
    }
}

int main()
{
    std::mutex m_a, m_b, m_c;
    int a, b, c = 1;

    std::vector<std::thread> threads;
    for (unsigned i = 0; i < 1; ++i)
        threads.emplace_back(update, std::ref(m_a), std::ref(m_b), std::ref(m_b), std::ref(a), std::ref(b), std::ref(c));

    for (auto& i: threads)
        i.join();

    std::cout << a << "'th and " << a+1 << "'th Fibonacci numbers: "
              << b << " and " << c << '\n';
}

So, as I said, I don't really understand why lock() causes the endless loop with such a chain of calls (lock -> try_lock -> unlocked).


Solution

  • Change std::ref(m_b), std::ref(m_b) to std::ref(m_b), std::ref(m_c). Copy/paste typo.

    Your std::lock is trying to lock m_b twice.

    Other issues: you violate rule of 0/3/5. You have multiple different near-identical lock fiddling for lock/unlock code (refactor).