Qt多线程示例--并发数据处理
生活随笔
收集整理的這篇文章主要介紹了
Qt多线程示例--并发数据处理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在通信中,往往會(huì)遇到這樣的情況
當(dāng)接入N個(gè)子結(jié)點(diǎn),每個(gè)子結(jié)點(diǎn)向它的父結(jié)點(diǎn)發(fā)數(shù)據(jù),父節(jié)點(diǎn)來并發(fā)處理總子結(jié)點(diǎn)匯集的數(shù)據(jù)。
對(duì)于上述情況,我們經(jīng)常設(shè)計(jì)成多線程來并發(fā)接收數(shù)據(jù),將數(shù)據(jù)接收后排隊(duì)存入一個(gè)全局變量,再單獨(dú)開辟一個(gè)線程從這個(gè)全局變量讀取第一個(gè)數(shù)據(jù),處理完則移除第一個(gè)數(shù)據(jù)。Qt中的鏈表直接提供了一個(gè)takeFirst函數(shù),用while循環(huán)讀取,在讀取的時(shí)候加鎖,這樣的話就不會(huì)沖突了。
在這里我們?cè)O(shè)計(jì)了一個(gè)定時(shí)器來模擬子結(jié)點(diǎn)產(chǎn)生的數(shù)據(jù),開辟一個(gè)單獨(dú)的線程來讀取數(shù)據(jù)。
關(guān)鍵代碼:
#ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow> #include "qtimer.h" #include "thread.h" #include "app.h"QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACEclass MainWindow : public QMainWindow {Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:Ui::MainWindow *ui;QTimer *timer;Thread *thread;private slots:void writeOne();void readOne(QString txt);void on_btnTimer_clicked();void on_btnThread_clicked();void on_btnAppend_clicked(); }; #endif // MAINWINDOW_H #include "mainwindow.h" #include "ui_mainwindow.h" #include<QTimer>#include "qdatetime.h" #define _TIME_ qPrintable (QTime::currentTime().toString("now : hh:mm:ss:zzzz"))MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow) {ui->setupUi(this);setWindowTitle("并發(fā)數(shù)據(jù)處理的簡單舉例");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)));}MainWindow::~MainWindow() {delete ui; }void MainWindow::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");}}void MainWindow::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 MainWindow::on_btnAppend_clicked() {App::list.append(ui->lineEdit->text()); }void MainWindow::writeOne() {App::list.append(_TIME_); } void MainWindow::readOne(QString txt) {ui->txtOut->append(txt); } #ifndef THREAD_H #define THREAD_H #include "qthread.h" #include "qmutex.h" #include "app.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 // THREAD_H #include "thread.h" //#include "app.h"extern QList<QString> list;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);}stopped = false; } #ifndef APP_H #define APP_H#include<QList>class App{public:static QList<QString> list;};QList<QString> App::list = QList<QString>();#endif // APP_H參考文章:
https://blog.csdn.net/xu1129005165/article/details/81702924
總結(jié)
以上是生活随笔為你收集整理的Qt多线程示例--并发数据处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软设考试笔记--UML建模
- 下一篇: QThreadPool Class的翻译