Qt 多線程(QThreadPool)
開發(fā)環(huán)境 Qt5.5.1、Qt Creator 3.5.1

#include
#include
#include
#include
#include
#include
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) {
//注冊一個(gè)自定義類型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;
//線程的耗時(shí)操作
class MyRunnable :public QRunnable{
public:
MyRunnable(QObject* observer): m_observer(observer){
}
//Override
void run();
private:
//QPointer是為QObject對象提供保護(hù)指針的模板類
//一個(gè)保護(hù)指針 QPointer,行為類似正常的C++指針,只不過其在引用對象刪除之后會(huì)自動(dòng)設(shè)置為0. T必須是QObject的子類
//保護(hù)指針在你要保存別人創(chuàng)建的QObject對象,且有可能其已經(jīng)被刪除而你仍然持有其引用的時(shí)候非常有用。
//Qt也提供QSharedPointer,一個(gè)基于引用計(jì)數(shù)的共享指針對象實(shí)現(xiàn),其能使用單個(gè)指針用于維護(hù)一個(gè)引用集合
//就是說,如果指代的對象被刪除,則后面對該對象的所有操作不會(huì)執(zhí)行,因此也不會(huì)拋異常
QPointer
};
//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
};
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 r = m_runnableList.at(i); m_runnableList.removeAt(i); } } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug()<<"main thread "< qDebug()<<"main thread "< MyProcesser processer; processer.process(); processer.process(); return a.exec(); } Qt 任務(wù)調(diào)度 多線程
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。