Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use QMutex?

I'm newbie to Qt and I'm looking for multi-threading in Qt.
As I learned in Qt Documents, I defined two class for two thread:

#include <QThread>
#include <QMutex>

class thread_a : public QThread
{
    Q_OBJECT
public:
    explicit thread_a(QObject *parent = 0);
    int counter;

protected:
    void run();
};

And in CPP file:

#include "thread_a.h"

thread_a::thread_a(QObject *parent) :
    QThread(parent)
{
    counter=0;
}

void thread_a::run()
{
    counter++;
}

Second thread class is same, but with counter-- in run() method.
Then I run this two threads from main.ccp.

Now my question:
How can I share counter in thread_a and thread_b using QMutex?

like image 872
S.M.Mousavi Avatar asked Jan 23 '12 11:01

S.M.Mousavi


People also ask

How does QMutex work?

The QMutex() constructs a new mutex. The mutex is created in an unlocked state. If mode is QMutex::Recursive, a thread can lock the same mutex multiple times and the mutex won't be unlocked until a corresponding number of unlock() calls have been made. Otherwise a thread may only lock a mutex once.

How do I use mutex in CPP?

A mutex is initialized in the beginning of the main function. The same mutex is locked in the 'trythis()' function while using the shared resource 'counter'. At the end of the function 'trythis()' the same mutex is unlocked. At the end of the main function when both the threads are done, the mutex is destroyed.


3 Answers

Instead of having the data within a thread, move the data outside the thread, protect it and then access it from both threads.

The following is a sketch, of what you could do:

class Counter
{
  public:
    Counter():mMutex(),mCounter(0){}
    int inc()
    {
      QMutexLocker ml(&mMutex);
      return mCounter++;
    }
    int dec()
      QMutexLocker ml(&mMutex);
      return mCounter--;
    }
  private:
    QMutex mMutex;
    int mCounter;
    Q_DISABLE_COPY(Counter)
};

class ThreadA : public QThread
{
  public:
    ThreadA(Counter* ctr);
  /* ... */
};

class ThreadB : public QThread
{
  public:
    ThreadB(Counter* ctr);
  /* ... */
};

The construct of Counter is often referred to as a Monitor, from Wikipedia (emphasis mine):

In concurrent programming, a monitor is an object or module intended to be used safely by more than one thread. The defining characteristic of a monitor is that its methods are executed with mutual exclusion. That is, at each point in time, at most one thread may be executing any of its methods. This mutual exclusion greatly simplifies reasoning about the implementation of monitors compared to reasoning about parallel code that updates a data structure.

In this specific case, a more efficient construct would be QAtomicInt. This gains atomicity from its use of special CPU instructions. This is a low level class that could be used to implement other threading constructs.


Edit - Complete Example

Using threads with shared state correctly is not trivial. You may want to consider using Qt signals/slots with queued connections or other message based systems.

Alternatively, other programming languages such as Ada support Threads and Monitors (protected objects) as native constructs.

Here is a complete working example. This is only sample code, don't use QTest::qSleep in real code.

objs.h

#ifndef OBJS_H
#define OBJS_H

#include <QtCore>

class Counter
{
    public:
        Counter(int init);
        int add(int v);
    private:
        QMutex mMutex;
        int mCounter;
        Q_DISABLE_COPY(Counter)
};

class CtrThread : public QThread
{
    Q_OBJECT
    public:
        CtrThread(Counter& c, int v);
        void stop();
    protected:
        virtual void run();
    private:
        bool keeprunning();
        Counter& mCtr;
        int mValue;
        bool mStop;
        QMutex mMutex;
};

#endif

objs.cpp

#include "objs.h"

Counter::Counter(int i):
    mMutex(),
    mCounter(i)
{}

int Counter::add(int v)
{
    QMutexLocker ml(&mMutex);
    return mCounter += v;
}

///////////////////////////////////////

CtrThread::CtrThread(Counter& c, int v):
    mCtr(c),
    mValue(v),
    mStop(false),
    mMutex()
{}

void CtrThread::stop()
{
    QMutexLocker ml(&mMutex);
    mStop = true;
}

void CtrThread::run()
{
    while(keeprunning())
    {
        mCtr.add(mValue);
    }
}

bool CtrThread::keeprunning()
{
    QMutexLocker ml(&mMutex);
    return ! mStop;
}

test.cpp

#include <QtCore>
#include <QTest>
#include "objs.h"

int main(int argc, char** argv)
{
    QCoreApplication app(argc, argv);

    qDebug() << "Initalising";

    Counter ctr(0);
    CtrThread thread_a(ctr, +1);
    CtrThread thread_b(ctr, -1);

    qDebug() << "Starting Threads";

    thread_a.start();
    thread_b.start();

    for (int i = 0; i != 15; ++i)
    {
        qDebug() << "Counter value" << ctr.add(0);
        QTest::qSleep(1000);
    }

    qDebug() << "Stopping Threads";

    thread_a.stop();
    thread_b.stop();
    thread_a.wait();
    thread_b.wait();

    qDebug() << "Finished";
    return 0;
}

test.pro

QT=core testlib
HEADERS=objs.h
SOURCES=test.cpp objs.cpp

Compile and run, you will see the value being printed, sample output:

Initalising
Starting Threads
Counter value 0
Counter value 11057
Counter value 28697
Counter value 50170
Counter value 60678
Counter value 73773
Counter value 84898
Counter value 96441
Counter value 118795
Counter value 135293
Counter value 146107
Counter value 158688
Counter value 169886
Counter value 201203
Counter value 212983
Stopping Threads
Finished
like image 189
Silas Parker Avatar answered Sep 28 '22 03:09

Silas Parker


OK, special thanks to @skyhisi for great solution for a real project.

I read @skyhisi post and more articles about QMutex and sharing variables. For education purpose i implemented a simple/clear sample of QMutex usage for sharing variable (this case counter).

class thread_a : public QThread
{
    Q_OBJECT
public:
    thread_a(QMutex*, int*);
    void shutdown();

private:
    QMutex* mutex;
    int* counter;
    bool isShutdownRequested;

protected:
    void run();
};

and in thread_a.cpp file:

thread_a::thread_a(QMutex * m, int* i)
{
    counter=i;
    isShutdownRequested=false;
    mutex=m;
}

void thread_a::run()
{
    isShutdownRequested=false;
    forever{
        //lock mutex for changing in shared variable
        mutex->lock();
        *counter=*counter+1;
        mutex->unlock();

        if(isShutdownRequested)
            break;
    }
}

void thread_a::shutdown()
{
    isShutdownRequested=true;
}

In myMainWindow::RunThreads(bool bl) slot:

int cnt=0;
QMutex mu;
thread* a=new thread_a(&mu, &cnt);
thread* b=new thread_b(&mu, &cnt);
a.start();
b.start();

In myMainWindow::~myMainWindow() deconstructor:

a->shutdown();
b->shutdown();

Thanks again @skyhisi

like image 32
S.M.Mousavi Avatar answered Sep 28 '22 03:09

S.M.Mousavi


I write a simple example referenced to the "help" of QMutex, in which two threads change a same number (as a Monitor). It also ref S.M.Mousavi's code. Here is code:

//main.cpp

#include <QCoreApplication>
#include "method.h"

int aNum=0;
QMutex aMutex;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int *p_no= &aNum;
    QMutex *p_Mu = &aMutex;

    method mThread1(p_Mu, p_no);
    method mThread2(p_Mu, p_no);

    mThread1.name = "one";
    mThread2.name = "two";

    mThread1.start();
    mThread2.start();

   return a.exec();
}

// method.h

#ifndef METHOD_H
#define METHOD_H

#include <QDebug>
#include <QThread>
#include <QtCore>
#include <QString>
#include <QMutex>


class method: public QThread
{
public:
    method(QMutex *mu, int *nu);
    void run();
    void method1();
    void method2();
    QString name;

private:
    int *number;
    QMutex *myMutex;
};

#endif // METHOD_H

//method.cpp #include "method.h"

method::method(QMutex *mu, int *nu)
{
    myMutex = mu;
    number = nu;
}


void method:: run()
{
    for (int i = 0; i<100; i++)
    {
        if(this->name == "one" )
        {
            this->method1();
        }
        else
        {
            this->method2();
        }
    }
}

void method::method1()
{
    myMutex->lock();
    *number += 1;
    qDebug()<<*number<<"---"<<this->name;
    myMutex->unlock();
}

void method ::method2()
{
    myMutex->lock();
    *number -= 1;
    qDebug()<<*number<<"---"<<this->name;
    myMutex->unlock();
}
like image 33
maroon912 Avatar answered Sep 28 '22 04:09

maroon912