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

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

 

程序运行截图如下:

Qt笔记-多线程检索数据库(单例多重锁,QtConcurrent::blockingMapped)_第1张图片

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

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

 

源码如下:

BigDataTest.h

#ifndef CONCURRENT_H
#define CONCURRENT_H

#include 
#include 


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

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

private:
    QList m_str;
};

#endif // CONCURRENT_H

MySQLInstance.h

#ifndef MYSQLINSTANCE_H
#define MYSQLINSTANCE_H

#include 

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

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 retList = QtConcurrent::blockingMapped(m_str, sqlResult);
    qDebug() << "返回结果";
    qDebug() << retList;
}

main.cpp

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

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

你可能感兴趣的:(C/C++,Qt,设计模式,数据库,mysql,c++,qt,QtConcurrent)