Qt-UI

Scoped Lock

QThread and QMutex | | Queued Connections

There is no “scoped lock” like boost::mutex::scoped_lock lock(mutex) in plain Qt, but it is easily setup:

class QScopedLock
{
   public:

   QScopedLock(QMutex &mutex)
   : mutex_(mutex)
   {
      mutex_.lock();
   }

   ~QScopedLock()
   {
      mutex_.unlock();
   }

   protected:

   QMutex &mutex_;
};

With that we rewrite run():

void run()
{
   QScopedLock lock(wok);

   put_ingredients();
   cook_and_stir();
   clean_wok();
}

Ok, I cheated: There is a scoped lock in Qt, but it is named QMutexLocker.

QThread and QMutex | | Queued Connections

Options: