司马懿的西山居

如果觉得有帮助,就顶顶顶一个,或者关注、关注、关注一下,转载请注明出处。...

Qt 多线程(QThreadPool)

开发环境 Qt5.5.1、Qt Creator 3.5.1 

#include <QCoreApplication>
#include <QObject>
#include <QDebug>
#include <QThread>
#include <QThreadPool>
#include <QPointer>

class MyRunnable;
//事件类
class MyEvent: public QEvent {
public:
    MyRunnable *m_runnable;
    QString m_message;
    MyEvent(MyRunnable *r, QString message): QEvent(eventType()), m_runnable(r), m_message(message) { }
    static QEvent::Type eventType(){
        if(s_eventType == QEvent::None) {
            //注册一个自定义类型1000-65535
            s_eventType = (QEvent::Type)registerEventType();
            //qDebug()<<"registerEventType "<<(int)s_eventType;
        }
        return s_eventType;
    }
private:
    static QEvent::Type s_eventType;
};
QEvent::Type MyEvent::s_eventType = QEvent::None;

//线程的耗时操作
class MyRunnable :public QRunnable{
public:
    MyRunnable(QObject* observer): m_observer(observer){
    }
    //Override
    void run();
private:
    //QPointer是为QObject对象提供保护指针的模板类
    //一个保护指针 QPointer,行为类似正常的C++指针,只不过其在引用对象删除之后会自动设置为0. T必须是QObject的子类
    //保护指针在你要保存别人创建的QObject对象,且有可能其已经被删除而你仍然持有其引用的时候非常有用。
    //Qt也提供QSharedPointer,一个基于引用计数的共享指针对象实现,其能使用单个指针用于维护一个引用集合
    //就是说,如果指代的对象被删除,则后面对该对象的所有操作不会执行,因此也不会抛异常
    QPointer<QObject> m_observer;
};
//Override
void MyRunnable::run() {
    QString message;
    message.append(QString::number((int)QThread::currentThreadId())+" start\r\n");
    message.append(QThread::currentThread()->objectName()+"\r\n");
    QThread::sleep(1);
    message.append(QString::number((int)QThread::currentThreadId())+" end\r\n");
    QCoreApplication::postEvent(m_observer, new MyEvent(this, message));
}

//主程序中的消息处理程序
class MyProcesser: public QObject {
public:
    MyProcesser() {
    }
    //Override
    bool event(QEvent *);
    void process();
    void abort();
private:
    QList<MyRunnable*> m_runnableList;

};
bool MyProcesser::event(QEvent *e) {
    if(e->type() == MyEvent::eventType()) {
        qDebug()<<"processer get event, type is "<<(int)e->type();
        qDebug()<<((MyEvent*)e)->m_message;
        m_runnableList.removeOne(((MyEvent*)e)->m_runnable);
    }
}
void MyProcesser::process() {
    MyRunnable* runnable = new MyRunnable(this);
    m_runnableList.append(runnable);
    //是否线程池替你删除
    //If auto-deletion is enabled, QThreadPool will automatically delete this runnable after calling run();
    //otherwise, ownership remains with the application programmer.
    runnable->setAutoDelete(false);
    QThreadPool::globalInstance()->start(runnable);
}
void MyProcesser::abort() {
    int size = m_runnableList.size();
    MyRunnable* r;
    for(int i=0;i<size;i++) {
        r = m_runnableList.at(i);
        m_runnableList.removeAt(i);
    }
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug()<<"main thread "<<QThread::currentThreadId()<<" start";
    qDebug()<<"main thread "<<QThread::currentThread()->objectName();
    MyProcesser processer;
    processer.process();
    processer.process();
    return a.exec();
}


            
阅读更多
版权声明:转载请注明出处——http://blog.csdn.net/chy555chy/article https://blog.csdn.net/chy555chy/article/details/51560234
个人分类: Qt 5.5
想对作者说点什么? 我来说一句

Qt 线程池(QThreadPool )的使用

2017年12月14日 4KB 下载

没有更多推荐了,返回首页

不良信息举报

Qt 多线程(QThreadPool)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭