日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Qt采用線程以隊(duì)列方式實(shí)現(xiàn)下發(fā)數(shù)據(jù)_C 語言

作者:中國(guó)好公民st ? 更新時(shí)間: 2022-11-23 編程語言

什么叫做隊(duì)列方式

在C++中隊(duì)列是一種常用的數(shù)據(jù)結(jié)構(gòu)之一,一種特殊的線性表,一般采用先進(jìn)先出的方式。

很多情況下,在做數(shù)據(jù)處理時(shí),會(huì)根據(jù)先來后到的原則進(jìn)行處理。對(duì)于少量數(shù)據(jù)來說,主進(jìn)程就可以很快完成,所以不需要用到開線程的方式。將處理處理部分封裝成一個(gè)函數(shù),直接調(diào)用就OK了!

假設(shè),數(shù)據(jù)處理的時(shí)間消耗很大時(shí),繼續(xù)使用主進(jìn)程處理的話,肯定會(huì)導(dǎo)致頁面卡死,為了避免頁面卡死,最常用的方式就是開線程。

在程序使用過程中肯定不止一個(gè)位置進(jìn)行數(shù)據(jù)處理,那么,多次調(diào)用數(shù)據(jù)處理時(shí),如何保證按照觸發(fā)順序進(jìn)行數(shù)據(jù)解析呢?

這是本篇文章中的重點(diǎn)~

想要按照觸發(fā)順序下發(fā),必須要對(duì)下發(fā)的數(shù)據(jù)進(jìn)行排隊(duì),這里用到的容器是list。方便添加、刪除。

容器:std::list m_list; //存儲(chǔ)數(shù)據(jù)

1.存儲(chǔ)需要處理的數(shù)據(jù)

std::lock_guard<std::mutex> lck(m_mutexRobotData); //上鎖添加數(shù)據(jù)
m_list.push_back(stData);

在進(jìn)行數(shù)據(jù)存儲(chǔ)時(shí),進(jìn)行上鎖處理,因?yàn)樵诰€程中每處理一條數(shù)據(jù),需要進(jìn)行刪除,防止出錯(cuò)。

2.開啟線程

m_pThreadSendCmd = std::thread(&QWidget::ProcessingThread, this);
m_pThreadSendCmd.detach();

這里用到的是C11方式開啟線程,有一個(gè)弊端,使用功能detach方式后,已經(jīng)與程序脫離了,想要控制線程的關(guān)閉,保守的做法是由參數(shù)來控制。

這里,我才用了bool值,當(dāng)bool = false時(shí),說明線程停止;true表示線程正在運(yùn)行。

那么,對(duì)上述開啟線程方式進(jìn)行修改,如下:

if (m_bRunningRobotCmd == false)
{
	//線程未開啟,開啟線程
	qDebug() << QStringLiteral("開啟一個(gè)新線程");
	m_bRunningRobotCmd = true;
	m_pThreadSendCmd = std::thread(&QWidget::ProcessingThread, this);
	m_pThreadSendCmd.detach();
}

3.線程中數(shù)據(jù)處理

void QWidget::ProcessingThread()
{
    while(m_bRunningRobotCmd)
    {
        //線程處理
        sleep(200);
    }
    //退出while循環(huán),表示線程結(jié)束
}

此時(shí),當(dāng)m_bRunningRobotCmd = true時(shí),表示線程一直在啟動(dòng),當(dāng)m_bRunningRobotCmd = false時(shí),立刻停止線程,這時(shí)又會(huì)遇到一個(gè)問題,正在運(yùn)行的線程中該如何停止呢?

單純的m_bRunningRobotCmd = false,很顯然停止的概率不大,此時(shí),對(duì)安全的做法,需要用互斥量的方式,進(jìn)行停止

對(duì)上述線程開啟進(jìn)行修改,如下:

void QWidget::ProcessingThread()
{
    std::lock_guard<std::mutex> lck(m_mutexControlThread); //上鎖添加數(shù)據(jù)
    while(m_bRunningRobotCmd)
    {
        //線程處理
        sleep(200);
    }
    //退出while循環(huán),表示線程結(jié)束
}

關(guān)閉線程方式,如下:

m_bRunningRobotCmd = false;
std::lock_guard<std::mutex> lck(m_mutexControlThread);  //插入數(shù)據(jù)之前,首先加鎖

首先將bool值更改,再進(jìn)行加鎖。

4.線程內(nèi)容實(shí)現(xiàn)邏輯

當(dāng)list容器中存在數(shù)據(jù)時(shí),需要獲取第一條數(shù)據(jù),處理后刪除第一條數(shù)據(jù)。

實(shí)現(xiàn)代碼如下:

if(m_list.size() != 0)
{
    //容器中存在有效數(shù)據(jù)
    std::lock_guard<std::mutex> lck(m_mutexRobotData); //上鎖添加數(shù)據(jù)
    if(m_list.size() != 0)
    {
        //獲取第一條有效數(shù)據(jù)
        stData stInfo = m_list.front();
        //數(shù)據(jù)處理
        //刪除第一條數(shù)據(jù)
        m_list.pop_front();
    }
    
}

使用互斥量的方式控制線程增加了安全性,防止崩潰問題。

原文鏈接:https://juejin.cn/post/7153420828959309855

欄目分類
最近更新