我从多线程开始,将对象移动到 QThread 时遇到了一些麻烦。我的 Controller 类实例化 Worker,为 Worker 创建必要的线程并将 Worker 移动到新线程。当工作线程启动时,工作线程上的计算就开始了。Worker 类包含一个 Dummy 对象,在 worker 计算期间使用(函数Dummy::doDummyStuff()
)。一切似乎都工作正常,除了doDummyStuff()
似乎是在主(控制器)线程而不是工作线程中执行的。这是因为 Worker 对象(以及因此 Dummy 对象)是首先在主线程中创建的吗?有没有办法在工作线程中移动 Worker 对象的所有类成员?
这是一个代码片段:
控制器.h
class Controller: public QObject
{
Q_OBJECT
public:
Controller(std::vector<double> _data, QString _fn);
void startControl();
private:
QThread workerThread_;
Worker worker_;
PostProcessing postProc_;
};
控制器.cpp
Controller::Controller(std::vector<double> _data, QString _fn): QObject(), workerThread_(), worker_(_data), postProc_()
{
QObject::connect(&workerThread_, SIGNAL(started()), &worker_, SLOT(doWork()));
QObject::connect(&worker_, SIGNAL(signalResultReady(double)), &postProc_, SLOT(update(double)));
}
void Controller::startControl()
{
worker_.moveToThread(&workerThread_);
workerThread_.start();
}
工人.h
class Worker: public QObject
{
Q_OBJECT
public:
Worker(std::vector<double> _coord);
signals:
void signalResultReady(double);
public slots:
void doWork();
private:
double res_;
std::vector<double> coord_;
Dummy dummyCreatedInWorker_;
};
工人.cpp
Worker::Worker(std::vector<double> _coord): QObject(), res_(0), coord_(_coord), dummyCreatedInWorker_()
{
}
void Worker::doWork()
{
qDebug() << "Worker thread ID" << this->thread();
for(unsigned int ii = 0; ii < coord_.size(); ii++)
{
res_ += coord_[ii];
dummyCreatedInWorker_.doDummyStuff();
emit signalResultReady(res_);
/* ....*/
}
}
虚拟::doDummyStuff
void Dummy::doDummyStuff()
{
qDebug() << "Doing dummy stuff from thread" << this->thread();
for(int ii = 0; ii < 10; ii++)
{
res_ += ii;
}
}