欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Qt進程和線程QProcess和QThread的使用

 更新時間:2023年06月05日 10:40:42   作者:Yengi  
本文主要介紹了Qt進程和線程QProcess和QThread的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1、進程

QProcess相當于管理進程的一個類。

.start()相當于fork + exec函數(shù)族

(1)頭文件

#include <QProcess>

(2)聲明和創(chuàng)建

QProcess *np = new QProcess;

(3)啟動進程

//直接啟動:
np->start("notepad");
//帶命令行參數(shù)啟動:
QString program = "cmd.exe";
QStringList arguments;
arguments << "/c dir&pause";
myProcess.start(program, arguments);

(4)如何獲取進程運行時候的一些數(shù)據(jù)

①信號:readyRead()

②讀取數(shù)據(jù):readAll();

(5)判斷進程的運行狀態(tài)

//進程狀態(tài)
enum ProcessState { NotRunning, Starting, Running }
//獲取進程狀態(tài)
state()

(6)關(guān)閉進程

//直接關(guān)閉
np->close();
//發(fā)送殺死信號:
np->kill();

注意:

發(fā)送信號的同時,進程不一定結(jié)束,一般要用到進程的一些同步函數(shù):

np->waitForFinished();//阻塞到進程結(jié)束
np->waitForStarted();//阻塞到進程開始
np->waitForReadyRead();//阻塞到可以從當前進程讀取數(shù)據(jù)
np->waitForBytesWritten();//阻塞到數(shù)據(jù)寫入進程

(7)如果要給一個正在運行的進程發(fā)送數(shù)據(jù)

write()

2、線程

源代碼——可執(zhí)行程序文件——進程(資源分配的基本單位 PCB)——線程(任務調(diào)度的基本單位)

pthread_create(pid, attr, func, arg);

QT線程有三種方式:

1.繼承QThread,重寫run函數(shù)

//自定義線程類的頭文件
//添加線程類頭文件
#include <QThread>
//在類聲明里面添加
Q_OBJECT
//重寫run函數(shù):線程任務函數(shù)
void run();
void MyThread::run()
{
? ? while(1)
? ? {
? ? ? ? QThread::sleep(1);
? ? ? ? qDebug()<<"son run";
? ? }
}
/*----------------------------------------------------------------------------------------*/
//在使用的地方
//添加自定義線程類的頭文件
#include "mythread.h"
//創(chuàng)建線程對象
MyThread *mth;
mth = new MyThread;
//開啟線程
mth->start();
//關(guān)閉線程
mth->terminate();
mth->wait();
//線程間的數(shù)據(jù)傳遞
//一般使用信號槽機制來實現(xiàn)

2.直接使用QThread + 自定義QObject派生類的方式

//一個繼承于QObject類的一個自定義類
class thread_object : public QObject
//自定義槽函數(shù)work()
public slots:
? ? void work();
void thread_object::work()
{
? ? while(1)
? ? {
? ? ? ? qDebug()<<"object thread run";
? ? ? ? QThread::sleep(1);
? ? }
}
//在主線程中創(chuàng)建一個普通QThread類的對象mth
QThread *mth;
mth = new QThread;
//在主線程中創(chuàng)建一個自定義類thread_object 的對象work
thread_object *work;
work = new thread_object;
//將w的實現(xiàn)移入線程mth的作用范圍
work->moveToThread(mth);
//用信號關(guān)聯(lián)w里面想要執(zhí)行的任務
//在主線程中聲明一個信號start_th();
signals:
? ? void start_th();
//關(guān)聯(lián)這個信號的槽函數(shù)為workwork();
connect(this, &MainWindow::start_th, work, &thread_object::work);
//開啟線程
mth->start();
emit start_th();

3.用線程池 + 自定義QRunnable派生類的方式

每一個QT程序都存在一個默認的線程池

default_pool = QThreadPool::globalInstance();

1.自定義一個繼承于抽象類QRunnable的類(不能添加Q_OBJECT)

class MyTask : public QRunnable
{
public:
    MyTask(QString name);
    void run();
    QString th_name;
};

2.實現(xiàn)run函數(shù)

void MyTask::run()
{
    while(1)
    {
        QThread::sleep(1);
        qDebug()<<"任務:【"<<this->th_name<<"】 正在運行...";
    }
}

3.創(chuàng)建一個線程池對象

QThreadPool *pool;
pool = new QThreadPool;

4.添加任務到線程池

//創(chuàng)建自定義的任務
MyTask *t = new MyTask(ui->lineEdit->text());
//將任務添加到線程池
pool->start(t);

5.線程池的相關(guān)函數(shù)

int activeThreadCount() const : 當前活躍的線程數(shù)量
void cancel(QRunnable *runnable):移除掉沒有運行的指定任務
void clear():移除掉所有沒有運行的任務
int expiryTimeout() const:如果有任務超時未執(zhí)行,可以通過這個函數(shù)自動讓其退出
int maxThreadCount() const:線程池可維護的最大的線程個數(shù)
void releaseThread():釋放保留線程
void reserveThread():保留線程
void setExpiryTimeout(int expiryTimeout):與expiryTimeout()函數(shù)一起用,設置超時時間
void setMaxThreadCount(int maxThreadCount):設置線程可管理的最大線程個數(shù)
void start(QRunnable *runnable, int priority = 0):添加線程到線程池
bool tryStart(QRunnable *runnable):嘗試啟動一個指定的線程
bool waitForDone(int msecs = -1):等待退出

到此這篇關(guān)于Qt進程和線程QProcess和QThread的使用的文章就介紹到這了,更多相關(guān)Qt QProcess QThread內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論