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 
#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");
    }
}

你可能感兴趣的:(qt)