日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt多线程示例--并发数据处理

發布時間:2025/3/15 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt多线程示例--并发数据处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在通信中,往往會遇到這樣的情況

當接入N個子結點,每個子結點向它的父結點發數據,父節點來并發處理總子結點匯集的數據。

對于上述情況,我們經常設計成多線程來并發接收數據,將數據接收后排隊存入一個全局變量,再單獨開辟一個線程從這個全局變量讀取第一個數據,處理完則移除第一個數據。Qt中的鏈表直接提供了一個takeFirst函數,用while循環讀取,在讀取的時候加鎖,這樣的話就不會沖突了。

在這里我們設計了一個定時器來模擬子結點產生的數據,開辟一個單獨的線程來讀取數據。

關鍵代碼:

#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("并發數據處理的簡單舉例");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

總結

以上是生活随笔為你收集整理的Qt多线程示例--并发数据处理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。