網(wǎng)站首頁 編程語言 正文
QT中使用線程可以提高工作效率。
要使用線程要經(jīng)過一下四個(gè)步驟:
(1)先創(chuàng)建一個(gè)c++ class文件,記得繼承Thread,創(chuàng)建步驟如下:
a、第一步
b、第二步
(2)自定義一個(gè)run函數(shù),以后啟動線程的時(shí)候,程序就會跳轉(zhuǎn)到run函數(shù)中
void run();
(3)初始化線程
HDThread mythread = new HDThread();
(4)啟動線程
mythread->start();
下面來看看線程使用的具體列子:
線程頭文件hdthread.h:
#ifndef HDTHREAD_H
#define HDTHREAD_H
#include <QThread>
#include <QLabel>
#include <QMutex>
class HDTHread : public QThread
{
public:
HDTHread(QMutex* mtex,QObject *parent = 0);
void run();//自定義的run函數(shù)
void setLabel(QLabel *lb);
private:
QLabel *label;
QMutex *mutex; //互斥鎖
};
#endif // HDTHREAD_H
主函數(shù)的頭文件threadqt.h
#ifndef THREADQT_H
#define THREADQT_H
#include <QMainWindow>
#include <hdthread.h>
#include <writefile.h>
#include <QMutex>
#include <QSemaphore>
namespace Ui {
class ThreadQt;
}
class ThreadQt : public QMainWindow
{
Q_OBJECT
public:
explicit ThreadQt(QWidget *parent = 0);
~ThreadQt();
//定義靜態(tài)的信號類
static QSemaphore *sp_A;
static QSemaphore *sp_B;
private slots:
void on_pushButton_clicked();
private:
Ui::ThreadQt *ui;
HDTHread *thread; //hdtread類,里面繼承了線程
WriteFile *writethread;
QMutex mutex;//定義互斥鎖類
};
#endif // THREADQT_H
源文件hdthread.cpp:
#include "hdthread.h"
#include <QDebug>
#include <threadqt.h>
HDTHread::HDTHread(QMutex *mtex, QObject *parent):QThread(parent)//構(gòu)造函數(shù),用來初始化
{
this->mutex = mtex;
}
void HDTHread::setLabel(QLabel *lb)
{
this->label = lb;
}
void HDTHread::run() //啟動線程時(shí)執(zhí)行的函數(shù)
{
while(true)
{
qint64 data = qrand()%1000; //取隨機(jī)數(shù)
//this->mutex->lock();//上鎖
ThreadQt::sp_A->acquire();//請求信號
this->label->setText(QString::number(data));
sleep(1);
ThreadQt::sp_B->release();//釋放信號
//this->mutex->unlock();//解鎖
qDebug()<<"hello Qt"<<data;
}
}
源文件threadqt.cpp
#include "threadqt.h"
#include "ui_threadqt.h"
//初始化靜態(tài)變量
QSemaphore *ThreadQt::sp_A = NULL;
QSemaphore *ThreadQt::sp_B = NULL;
ThreadQt::ThreadQt(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ThreadQt)
{
ui->setupUi(this);
//創(chuàng)建信號對象
sp_A = new QSemaphore(1);
sp_B = new QSemaphore(0);
}
ThreadQt::~ThreadQt()
{
delete ui;
}
void ThreadQt::on_pushButton_clicked()
{
thread = new HDTHread(&mutex); //初始化線程
thread->setLabel(ui->label);
thread->start();//開啟線程
writethread = new WriteFile(&mutex);
writethread->setLabel(ui->label);
writethread->start();
}
大家也看到了,此處的線程也用到了互斥鎖(信號量)保證線程讀寫數(shù)據(jù)時(shí)不出現(xiàn)錯(cuò)誤,這里大家可以看一下具體實(shí)現(xiàn)的代碼
this->mutex->lock();//上鎖
ThreadQt::sp_A->acquire();//請求信號
this->label->setText(QString::number(data));
sleep(1);
ThreadQt::sp_B->release();//釋放信號
this->mutex->unlock();//解鎖
原文鏈接:https://blog.csdn.net/m0_60259116/article/details/128334992
相關(guān)推薦
- 2022-07-15 VS2022?Git提交代碼的實(shí)現(xiàn)_C 語言
- 2022-06-22 Git常用命令匯總_其它綜合
- 2022-06-28 C++哈希表之線性探測法實(shí)現(xiàn)詳解_C 語言
- 2022-04-08 Android如何使用GestureDetector進(jìn)行手勢檢測詳解_Android
- 2022-12-04 Golang如何快速構(gòu)建一個(gè)CLI小工具詳解_Golang
- 2022-05-27 Android實(shí)現(xiàn)拼圖游戲_Android
- 2022-04-18 python使用py2neo創(chuàng)建neo4j的節(jié)點(diǎn)和關(guān)系_python
- 2022-11-22 Python?Counting?Bloom?Filter原理與實(shí)現(xiàn)詳細(xì)介紹_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支