QThread实现多线程

线程是系统调度的最小单元,一个进程可以包含多个线程,作为任务的真正运作者,有自己的栈(Stack)、寄存器(Register)、本地存储(Thread Local)等,但是会和进程内其他线程共享文件描述符、虚拟地址空间等。

进程可以多了解QProcess,并且QT提供了多种进程间通信的方式,包括TCP/IP,共享内存,D-BUS,QT通信协议,你可以分别通过帮助文档了解QtNetwork模块,QSharedMemory类,D-BUS(QtDBus模块),QCopChannel来使用它们,并且是可以加锁来同步的

多线程可以参考:QThread类
互斥量,信号量参考:QMutex类,QSemaphore类 

 

QT创建一个线程作为消费者,并用QTimer模拟生产者,实现有序的输出。

1、头文件

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QMutex>
#include <QTimer>
#include <QWaitCondition>

class MyThread : public QThread
{
public:
    explicit MyThread();
    ~MyThread();

protected:
    void run();
private:
    QMutex* m_mutex;
    int m_test;
    QTimer* m_timer;
    QWaitCondition* m_wait;
    int m_last_test;
};

#endif // MYTHREAD_H

 2、cpp文件

#include "mythread.h"

#include <iostream>

using namespace std;

MyThread::MyThread(): QThread (),m_mutex(new QMutex()),m_test(0),m_timer(new QTimer(this)),m_wait(new QWaitCondition),m_last_test(-1)
{
    //使用timer线程作为生产者
    m_timer->start(10000);
    connect(m_timer, &QTimer::timeout, this, [this] {
        m_mutex->lock();
        m_test ++;
        m_wait->wakeAll();
        m_mutex->unlock();
        cout << "Timer thread:" << currentThreadId() << " is "
             << (this->isFinished() ? "Finished" : "Unfinished") << endl;
//        this->start();
    });
    cout << "Create thread: " << currentThreadId() << endl;
}

MyThread::~MyThread()
{
    this->wait();
    delete m_mutex;
    m_timer->stop();
    delete m_timer;
    delete m_wait;
    cout << "this is ~" << endl;
}

void MyThread::run()
{
    while (1) {
        //生产资源不足时,消费者线程阻塞
        m_mutex->lock();
        if(m_test == m_last_test) {
            cout << "Test no change" << endl;
            //m_mutex最初必须是锁状态,之后被解锁,进入阻塞状态,直到线程被唤醒
            m_wait->wait(m_mutex);
        }
        m_mutex->unlock();

        //作为消费者,输出m_test
        m_mutex->lock();
        cout << "This is run " << m_test << " " << currentThreadId() << endl;
        m_last_test = m_test;
        this->sleep(5);
        m_mutex->unlock();
    }
}

3、main文件

#include <QCoreApplication>
#include "mythread.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    MyThread thread;
    thread.start();

    return a.exec();
}

4、输出结果

Create thread: 0000000000004D18
This is run 0 0000000000005E00
Test no change
Timer thread:0000000000004D18 is Unfinished
This is run 1 0000000000005E00
Test no change
Timer thread:0000000000004D18 is Unfinished
This is run 2 0000000000005E00
Test no change
Timer thread:0000000000004D18 is Unfinished
This is run 3 0000000000005E00
Test no change
Timer thread:0000000000004D18 is Unfinished
This is run 4 0000000000005E00
Test no change
Timer thread:0000000000004D18 is Unfinished
This is run 5 0000000000005E00
Test no change
Timer thread:0000000000004D18 is Unfinished
This is run 6 0000000000005E00
Test no change
Timer thread:0000000000004D18 is Unfinished
This is run 7 0000000000005E00
Test no change
Timer thread:0000000000004D18 is Unfinished
This is run 8 0000000000005E00

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值