Search code examples
c++multithreadingqtqtconcurrentqmutex

QMutex: destroying locked mutex


Given the following code:

#include <chrono>
#include <ctime>
#include <functional>
#include <iostream>
#include <thread>
#include <utility>

#include <QFuture>
#include <QMutex>
#include <QWaitCondition>
#include <QtConcurrent>

class Async
{
public:
    Async() = default;
    Async(const Async&) = delete;
    Async(Async&&) = delete;

    ~Async() = default; //{ m_mutex.unlock(); }

    Async& operator=(const Async&) = delete;
    Async& operator=(Async&&) = delete;

    template<typename t_result>
    QFuture<bool> operator()(
          std::function<t_result()>&& p_function,
          std::chrono::milliseconds p_timeout,
          t_result* p_result)
    {
        QtConcurrent::run([this, p_function, p_result]() {
            *p_result = p_function();
            std::cout << time(nullptr) << " waking" << std::endl;
            m_cond.wakeAll();
    });

    return QtConcurrent::run([this, p_timeout]() {
           std::cout << time(nullptr) << " starting to wait for "
                     << p_timeout.count() << " ms" << std::endl;
           m_mutex.lock();
           bool wait =
               m_cond.wait(&m_mutex, 
                           static_cast<unsigned 
                                       long>(p_timeout.count()));
           std::cout << time(nullptr)
                     << ", finished waiting = " 
                     << (wait ? "T" : "F") 
                     << std::endl;
           if (wait) {
               return false;
           }
           return true;
    });
  }

private:
    QMutex m_mutex;
    QWaitCondition m_cond;
};

int main()
{
  Async async;

  char letter = 'z';

  std::function<char()> f1 = []() -> char {
      std::this_thread::sleep_for(std::chrono::seconds(4));
      return 'a';
  };

  std::cout << "1: " << time(nullptr) << std::endl;
  QFuture<bool> result =
    async(std::move(f1), std::chrono::milliseconds(3999), 
          &letter);

  std::cout << "2: " << time(nullptr) << std::endl;

  std::this_thread::sleep_for(std::chrono::seconds(8));

  std::cout << "3: " << time(nullptr) << std::endl;

  if (result.result()) {
    std::cout << "timeout, letter = " << letter;
  } else {
    std::cout << "NO timeout, letter = " << letter;
  }
  std::cout << std::endl;

  return 0;
}

Finally... 8), when I run it, all the cout print what is expected, but I get a QMutex: destroying locked mutex at the end of the execution. Since I am getting the message finished waiting, the m_cond.wait is executed, therefore (I thought), m_mutex would be unlocked. But it does not seem so.

If I use ~Async() { m_mutex.unlock(); }, I do not get the message, but I do not think it should be done this way.

Could anyone, please, explain why the mutex is not being released?

Thanks a lot!


Solution

  • When a runnable wait on a condition variable using an exclusive mutex, it holds the lock on the mutex when the wait is over (timeout or not).

    Which means you have to explicitly unlock the mutex

    bool wait = m_cond.wait(&m_mutex,static_cast<unsigned long>(p_timeout.count()));
    m_mutex.unlock();
    if (wait) {
        return false;
    }
    return true;