Qt多线程QThread使用

 第一种创建线程的方法:di

#include <QCoreApplication>
#include<QThread>
#include<QDebug>
#include<QMutex>

QMutex mtx;
int num = 200;
int sale = 0;
class myThreads:public QThread{
public:
    void run() override{
        //qDebug()<<"currentThreadId:"<<this->currentThreadId();
        while (sale<num) {
             mtx.lock();
                if(sale<num){
                   sale ++;
                  qDebug()<<"currentThreadId:"<<this->currentThreadId()<<" sale:"<<sale;
                }

             mtx.unlock();
        }

        this->quit();

    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    //创建4个线程
    myThreads t1;
    myThreads t2;
    myThreads t3;
    myThreads t4;
    t1.start();
    t2.start();
    t3.start();
    t4.start();

    return a.exec();
}

 第二种创建线程的方法:

#include <QCoreApplication>
#include<QThread>
#include<QDebug>
#include<QMutex>
#include<QRunnable>
#include<QThreadPool>
QMutex mtx;
int num = 200;
int sale = 0;
class myThreads:public QThread{
public:
    void run() override{
        //qDebug()<<"currentThreadId:"<<this->currentThreadId();
        while (sale<num) {
             mtx.lock();
                if(sale<num){
                   sale ++;
                  qDebug()<<"currentThreadId:"<<this->currentThreadId()<<" sale:"<<sale;
                }

             mtx.unlock();
        }

        this->quit();

    }
};

class myThread2:public QRunnable{
   public:
    void run(){
            qDebug()<<"currentThreadId = "<<QThread::currentThreadId();
    }

};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    //创建4个线程
    //myThreads t1;
    //myThreads t2;
    //myThreads t3;
    //myThreads t4;
    //t1.start();
    //t2.start();
    //t3.start();
    //t4.start();

     myThread2 *t5 = new myThread2();
     QThreadPool::globalInstance()->start(t5);




    return a.exec();
}

缺点:这种继承于QRunnable创建多线程的方式,不能使用信号和槽机制;

优点:线程池自动管理对象;

第三种创建多线程的方式:

派生于QObject,使用moveToObject方法;

第四种创建多线程的方式:

#include <QCoreApplication>
#include<QMutex>
#include<QtConcurrent>
#include<QDebug>

int tickes =100;
QMutex mtx;
void  process_1(){

    while(tickes>=0){
        mtx.lock();
        if(tickes>=0){
            QThread::msleep(100);
            qDebug()<<"process_1 is getting "<<tickes;
            tickes--;
        }
       mtx.unlock();

    }


}

void  process_2(){

    while(tickes>=0){
        mtx.lock();
        if(tickes>=0){
            QThread::msleep(100);
            qDebug()<<"process_2 is getting "<<tickes;
            tickes--;
        }
       mtx.unlock();

    }


}

void  process_3(){

    while(tickes>=0){
        mtx.lock();
        if(tickes>=0){
            QThread::msleep(100);
            qDebug()<<"process_3 is getting "<<tickes;
            tickes--;
        }
       mtx.unlock();

    }


}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);


    QtConcurrent::run(process_1);
    QtConcurrent::run(process_2);
    QtConcurrent::run(process_3);

    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值