qt多线程示例+一种通用高并发数据处理最简单思路

波特率115200=115200位/s=11520字节/s(无校验位)
多个串口和网络发来的数据,需处理。波特率9600约等于1000个字节/s的数据,
尤其在ARM上直接卡–>多线程–>一个线程收数据—>一个线程处理数据
当协议一样,要将串口数据解析转为网络端口监听,之前可用tcp通信处理。
还可以:各种数据接收后排队存入一个全局变量,再单独开辟一个线程从这个全局变量读取第一个数据,处理完则移除第一个数据。Qt中的链表直接提供了一个takeFirst函数,用while循环读取,在读取的时候加锁,这样的话就不会冲突了。

独立处理数据线程:
#ifndef TEST_H
#define TEST_H
#include "qthread.h"
#include "qmutex.h"
class Thread : public QThread
{
    Q_OBJECT
public:
    Thread();
    ~Thread();
    void stop();
protected:
    void run();
private:
    QMutex mutex;
    volatile bool stopped;
signals:
    void readOne(QString txt);
};
#endif // TEST_H
#include "thread.h"
#include "app.h"
Thread::Thread()
{
stopped=false;
}
Thread::~Thread()
{
}
void Thread::stop()
{
    stopped=true;
}
void Thread::run()
{
    while(!stopped){
        mutex.lock();
        if (App::list.count()>0){            
            QString txt=App::list.takeFirst();
            emit readOne(txt);
        }
        mutex.unlock();
        msleep(1);//不加这句CPU占用率高达50%
    }
    stopped=false;
}
主界面:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "thread.h"
#include "qtimer.h"
namespace Ui {
class frmMain;
}
class frmMain : public QWidget
{
    Q_OBJECT
public:
    explicit frmMain(QWidget *parent = 0);
    ~frmMain();
private slots:
    void writeOne();
    void readOne(QString txt);
    void on_btnAppend_clicked();
    void on_btnThread_clicked();
    void on_btnTimer_clicked();
private:
    Ui::frmMain *ui;
    QTimer *timer;
    Thread *thread;
};
#endif // WIDGET_H
#include "frmmain.h"
#include "ui_frmmain.h"
#include "app.h"
#include "qdatetime.h"
#include "qdesktopwidget.h"
#define _TIME_ qPrintable (QTime::currentTime().toString("now : hh:mm:ss:zzz"))
frmMain::frmMain(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::frmMain)
{
    ui->setupUi(this);
    this->showMaximized();
    timer=new QTimer(this);
    timer->setInterval(50);
    connect(timer,SIGNAL(timeout()),this,SLOT(writeOne()));
    thread=new Thread;
    connect(thread,SIGNAL(readOne(QString)),this,SLOT(readOne(QString)));
}
frmMain::~frmMain()
{
    delete ui;
}
void frmMain::writeOne()
{
    App::list.append(_TIME_);
}
void frmMain::readOne(QString txt)
{
    ui->txtOut->append(txt);
}
void frmMain::on_btnAppend_clicked()
{
    App::list.append(ui->txtIn->text());
}
void frmMain::on_btnThread_clicked()
{
    if (ui->btnThread->text()=="start thread"){
        thread->start();
        ui->btnThread->setText("stop thread");
        ui->txtOut->append("start thread ok");
    }else{
        thread->stop();
        ui->btnThread->setText("start thread");
        ui->txtOut->append("stop thread ok");
    }
}
void frmMain::on_btnTimer_clicked()
{
    if (ui->btnTimer->text()=="start timer"){
        timer->start();
        ui->btnTimer->setText("stop timer");
        ui->txtOut->append("start timer ok");
    }else{
        timer->stop();
        ui->btnTimer->setText("start timer");
        ui->txtOut->append("stop timer ok");
    }
}
  • 2
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Qt Creator 多线程读取文件到程序显示 利用QT Creator多任务读取一个文档到程序里 为了防止直接读取文件里的内容太大而发生卡顿,于是多线程读取将更高效的解决这个问题。 效果图如下: 其中pro文件无需改动,默认就好,头文件h里面的内容为 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include #include QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MyObj; class MyObj : public QObject { Q_OBJECT public: MyObj(); //新的线程 signals: void toLine(QString line); private slots: void doWork(); }; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void appendText(QString); //定义一个槽 private: Ui::MainWindow *ui; QThread *t; MyObj *obj; }; #endif // MAINWINDOW_H 而MAIN主文件的内容为了防止中文乱码做了如下修改: #include "mainwindow.h" #include #include int main(int argc, char *argv[]) { QApplication a(argc, argv); //设置中文字体 防止乱码 a.setFont(QFont("Microsoft Yahei", 9)); //设置中文编码 #if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("GBK"); #else QTextCodec *codec = QTextCodec::codecForName("UTF-8"); #endif QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec); QTextCodec::setCodecForTr(codec); #else QTextCodec *codec = QTextCodec::codecForName("UTF-8"); QTextCodec::setCodecForLocale(codec); #endif MainWindow w; w.show(); return a.exec(); } 接下来重点来了,源文件CPP里为 #include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include #include #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); t = new QThread(); //QThread obj = new MyObj(); obj->moveToThread(t); qDebug()<<"main thread:"<<QThread::currentThread(); connect(t,SIGNAL(started()), obj, SLOT(doWork())); connect(obj,SIGNAL
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

穿着帆布鞋也能走猫步

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

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

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

打赏作者

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

抵扣说明:

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

余额充值