Qt笔记-多线程检索数据库(单例多重锁,QtConcurrent::blockingMapped)

这里主要是个事例,并不涉及业务逻辑处理。在此记录下。

 

程序运行截图如下:

这里有下面这个类BigDataTest对数据进行的类。

MySQLInstance采用单例双重锁模式对数据库进行操作。

 

源码如下:

BigDataTest.h

#ifndef CONCURRENT_H
#define CONCURRENT_H

#include <QObject>
#include <QList>


class BigDataTest : public QObject
{
    Q_OBJECT
public:
    BigDataTest(QObject *parent = 0);
    void run();

protected:
    bool connectMySQL();
    friend QString sqlResult(const QString &cmd);

private:
    QList<QString> m_str;
};

#endif // CONCURRENT_H

MySQLInstance.h

#ifndef MYSQLINSTANCE_H
#define MYSQLINSTANCE_H

#include <QSqlDatabase>

class MySQLInstance
{
public:
    static MySQLInstance *getInstance();
    QString execSql(const QString &cmd);

protected:
    MySQLInstance();
    void connectMySQL();

private:
    static MySQLInstance *m_instance;
    QSqlDatabase m_db;
};

#endif // MYSQLINSTANCE_H

BigDataTest.cpp

#include "BigDataTest.h"
#include "MySQLInstance.h"
#include <QDebug>
#include <QtConcurrent>
#include <QSqlQuery>

BigDataTest::BigDataTest(QObject *parent) : QObject(parent)
{

    m_str << "SELECT 1 + 1" << "SELECT 100 * 100 * 100 / 10" << "SELECT 100 + 99" << "SELECT 1 + 1"
          << "SELECT 1 + 1 + 500" << "SELECT 1 + 1 + 1000" << "SELECT 1999 - 199" << "SELECT 1999 + 10"
          << "SELECT 1 + 1 + 8888" << "SELECT 1 + 1 + 788";
}

QString sqlResult(const QString &cmd)
{
    qDebug() << "处理SQL语句进程 : " << QThread::currentThread();
    MySQLInstance *instance = MySQLInstance::getInstance();
    return instance->execSql(cmd);
}


void BigDataTest::run()
{
    QList<QString> retList = QtConcurrent::blockingMapped(m_str, sqlResult);
    qDebug() << "返回结果";
    qDebug() << retList;
}

main.cpp

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

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

    BigDataTest bigDataTest;
    bigDataTest.run();

    return a.exec();
}

MySQLInstance.cpp

#include "MySQLInstance.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QMutex>
#include <QVariant>

MySQLInstance *MySQLInstance::m_instance = nullptr;
QMutex mt;

MySQLInstance *MySQLInstance::getInstance()
{
    if(m_instance == nullptr){

        mt.lock();
        if(m_instance == nullptr){

            m_instance = new MySQLInstance();
            mt.unlock();
            return m_instance;
        }
        mt.unlock();
    }

    return m_instance;
}

QString MySQLInstance::execSql(const QString &cmd)
{
    if(m_instance == nullptr){

        Q_ASSERT(!"m_instance == nullptr");
    }


    //简单点,返回值只有一个
    mt.lock();
    QSqlQuery query(m_db);
    if(!query.exec(cmd)){

        mt.unlock();
        qDebug() << "error:" << query.lastError().text();
        return "error";
    }
    mt.unlock();

    QString ret = "none";
    while(query.next()){

        ret = query.value(0).toString();
    }


    return ret;
}

MySQLInstance::MySQLInstance()
{
    connectMySQL();
}

void MySQLInstance::connectMySQL()
{
    m_db = QSqlDatabase::addDatabase("QMYSQL");
    m_db.setHostName("127.0.0.1");
    m_db.setPort(3306);
    m_db.setDatabaseName("mysql");
    m_db.setUserName("root");
    m_db.setPassword("root");
    m_db.open();
}

程序打包下载地址:

https://github.com/fengfanchen/Qt/tree/master/DealWithBigData

©️2020 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值