c++ - 如何使用 QMutex?

标签 c++ multithreading qt mutex qmutex

我是 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();
};

在 CPP 文件中:

#include "thread_a.h"

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

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

第二个线程类相同,但在 run() 方法中使用 counter--
然后我从 main.ccp 运行这两个线程。

现在我的问题是:
如何使用 QMutexthread_athread_b 中共享 counter

最佳答案

不要将数据放在线程中,而是将数据移出线程,保护它,然后从两个线程中访问它。

以下是你可以做什么的草图:

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);
  /* ... */
};

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.

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


编辑 - 完整示例

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

或者,其他编程语言(例如 Ada)支持线程和监视器( protected 对象)作为 native 构造。

这是一个完整的工作示例。 这只是示例代码,请勿在实际代码中使用 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

#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

编译运行,你会看到正在打印的值,示例输出:

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

关于c++ - 如何使用 QMutex?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8971168/

相关文章:

c++ - 如何在不调用析构函数的情况下重新分配 vector ?

multithreading - 如何测试跨线程队列?

qt - 用QAudioOutput播放ulaw文件

android - 如何使用 AWS C++ SDK 为 Android 构建 Qt 应用程序?

java - org.openqa.selenium.remote.UnreachableBrowserException

c++ - 使用名为 ASGE 的 C++ 框架,我的 Sprite 卡在屏幕边缘

c++ - 在嵌入式 Linux 上使用 C++ Std Lib 时出现异常段错误

c++ - 为什么 emplace_back 需要移动构造函数

java - 线程局部仅初始化为零

java - Spring和多线程