C++ 如何使用 QMutex?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8971168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:24:48  来源:igfitidea点击:

How to use QMutex?

c++multithreadingqtmutexqmutex

提问by S.M.Mousavi

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:

我是 Qt 的新手,我正在寻找 Qt 中的多线程。
正如我在Qt Documents中学到的,我为两个线程定义了两个类:

#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:

在 CPP 文件中:

#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.

第二个线程类相同,但使用counter--inrun()方法。
然后我从main.ccp.

Now my question:
How can I share counterin thread_aand thread_busing QMutex?

现在我的问题:
我如何可以共享counterthread_athread_b使用QMutex

回答by Silas Parker

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 Counteris often referred to as a Monitor, from Wikipedia (emphasis mine):

的构造Counter通常被称为Monitor,来自维基百科(强调我的):

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.

在这种特定情况下,更有效的构造是QAtomicInt. 这通过使用特殊的 CPU 指令获得原子性。这是一个低级类,可用于实现其他线程构造。



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.

正确使用具有共享状态的线程并非易事。您可能需要考虑将 Qt 信号/插槽与排队连接或其他基于消息的系统一起使用。

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

或者,其他编程语言(例如 Ada)支持线程和监视器(受保护的对象)作为本机构造。

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

这是一个完整的工作示例。这只是示例代码,请勿QTest::qSleep在实际代码中使用。

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

测试.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

回答by S.M.Mousavi

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

好的,特别感谢@skyhisi 为实际项目提供了出色的解决方案。

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).

我阅读了@skyhisi 帖子和更多关于 QMutex 和共享变量的文章。出于教育目的,我实现了一个简单/清晰的 QMutex 使用示例,用于共享变量(本例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.cppfile:

并在thread_a.cpp文件中:

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:

myMainWindow::RunThreads(bool bl)插槽中:

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:

myMainWindow::~myMainWindow()解构器中:

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

Thanks again @skyhisi

再次感谢@skyhisi

回答by maroon912

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:

我写了一个引用 QMutex 的“帮助”的简单示例,其中两个线程更改了相同的数字(作为监视器)。它还引用了 SMMousavi 的代码。这是代码:

//main.cpp

//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

// 方法.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.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();
}