QT之使用QMutex/ QMutexLocker互斥量同步线程小例子

    接上一篇,在多线程中基本上都需要解决线程同步问题,本篇文章主要将介绍如何使用QMutex/ QMutexLocker互斥量来同步线程。

    首先,先回顾下Qt官网QThreads general usage介绍,其中有一段基本上介绍了其使用方法,如下:

    The main thing in this example to keep in mind when using a QThread is that it's not a thread. It's a wrapper around a thread object. This wrapper provides the signals, slots and methods to easily use the thread object within a Qt project. To use it, prepare a QObject subclass with all your desired functionality in it. Then create a new QThread instance, push the QObject onto it using moveToThread(QThread*) of the QObject instance and call start() on the QThread instance. That's all. You set up the proper signal/slot connections to make it quit properly and such, and that's all.

    现在,直接上售票的小例子:

    Step1. 按Qt介绍的用法:先创建一个继承QObject的类,把要执行的函数包装进里面:

.h 文件代码如下:

#ifndef TICKETSELLER_H
#define TICKETSELLER_H

#include <QObject>
#include <QThread>
#include <QMutex>
#include <string>

class TicketSeller : public QObject
{
public:
    TicketSeller();

    ~TicketSeller();

public slots:
    void sale();

public:
    int* tickets;

    QMutex* mutex;

    std::string name;
};

#endif // TICKETSELLER_H

.cpp 文件代码如下:

#include "ticketseller.h"
#include <iostream>

TicketSeller::TicketSeller()
{
    tickets = 0;
    mutex = NULL;
}

TicketSeller::~TicketSeller()
{

}

void TicketSeller::sale()
{
    while((*tickets) > 0)
    {
        mutex->lock();
        std::cout << name << " : " << (*tickets)-- << std::endl;
        mutex->unlock();
    }

    /*while((*tickets) > 0)
    {
        QMutexLocker locker(mutex);
        std::cout << name << " : " << (*tickets)-- << std::endl;
        locker.unlock();
    }*/
}

    Step2. 按Qt介绍的用法:创建一个实例,使用moveToThread将对象移至QThread内,并连接信号槽,最后调用start()方法:

main.cpp 文件代码如下:

#include <QCoreApplication>
#include <QMutex>
#include <QThread>
#include <string>
#include "ticketseller.h"

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

    int ticket = 100;
    QMutex mutex;

    /*创建设置线程1*/
    //创建线程1
    QThread t1;
    TicketSeller seller1;
    //设置线程1
    seller1.tickets = &ticket;
    seller1.mutex = &mutex;
    seller1.name = "seller1";
    //将对象移动到线程
    seller1.moveToThread(&t1);

    /*创建设置线程2*/
    //创建线程2
    QThread t2;
    TicketSeller seller2;
    seller2.tickets = &ticket;
    seller2.mutex = &mutex;
    seller2.name = "seller2";
    //将对象移动到线程
    seller2.moveToThread(&t2);

    QObject::connect(&t1, &QThread::started, &seller1, &TicketSeller::sale);
    QObject::connect(&t2, &QThread::started, &seller2, &TicketSeller::sale);

    t1.start();
    t2.start();

    return a.exec();
}

    QT 中使用 QMutex/ QMutexLocker 比Window API CreateMutex 配合 WaitForSingleObject 同步线程要简单得多。



  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

l357630798

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值