这里someSlot函数主要输出了obj所在的线程和slot函数执行线程。
接着从SubThread又继承了3个线程类,分别是SubThread1, SubThread2, SubThread3.分别实现线程的run函数。定义如下:
- // define sub thread class 1
- class SubThread1 : public SubThread
- {
- Q_OBJECT
- public:
- SubThread1(QObject* parent=0);
- // reimplement run
- void run();
- };
- class SubThread2 : public SubThread
- {
- Q_OBJECT
- public:
- SubThread2(QObject* parent=0);
- // reimplement run
- void run();
- };
- class SubThread3 : public SubThread
- {
- Q_OBJECT
- public:
- SubThread3(QObject* parent=0);
- // reimplement run
- void run();
- };
在主程序中分别创建3个不同的线程并运行,查看运行结果。
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- SubThread1* t1 = new SubThread1(&a); //由主线程创建
- t1->start();
- SubThread2* t2 = new SubThread2(&a); //由主线程创建
- t2->start();
- SubThread3* t3 = new SubThread3(&a); //由主线程创建
- t3->start();
- return a.exec();
- }
