Qt 线程之-moveToThread 踩坑记录

文章探讨了在使用Qt创建子线程时,为何两个工作线程看似与主线程并行,但实际执行顺序受限的问题,发现问题在于向工作对象传递了父类(this)指针,导致串行执行。
摘要由CSDN通过智能技术生成

现象:

创建了两个子线程和两个工作任务类,并且将工作任务通过moveToThread转移到了子线程。

发现子线程和主线程是并行的,但是两个子线程却是串行的。按理说两个子线程和主线程都应该是并行的才对.

有问题的代码:

  LOG(INFO) <<"Proc Left";
  QThread*pWorkerThread = new QThread;
  CEyeProcWork *pWork = new CEyeProcWork(this);
  pWork->Init(strSrcNameL, strMaskNameL, strIrisNameL);
  pWork->moveToThread(pWorkerThread);
  QObject::connect(pWorkerThread, &QThread::started, pWork, &CEyeProcWork::doWorking, Qt::QueuedConnection);
  
  pWorkerThread->start();
  
  LOG(INFO) <<"Proc Right";
  QThread*pWorkerThread2 = new QThread;
  CEyeProcWork *pWork2 = new CEyeProcWork(this);
  pWork2->Init(strSrcNameR, strMaskNameR, strIrisNameR);
  pWork2->moveToThread(pWorkerThread2);
  QObject::connect(pWorkerThread2, &QThread::started, pWork2, &CEyeProcWork::doWorking, Qt::QueuedConnection);
  pWorkerThread2->start();

没问题的代码

  LOG(INFO) <<"Proc Left";
  QThread*pWorkerThread = new QThread;
  CEyeProcWork *pWork = new CEyeProcWork();
  pWork->Init(strSrcNameL, strMaskNameL, strIrisNameL);
  pWork->moveToThread(pWorkerThread);
  QObject::connect(pWorkerThread, &QThread::started, pWork, &CEyeProcWork::doWorking, Qt::QueuedConnection);
  pWorkerThread->start();
  
 LOG(INFO) <<"Proc Right";
 QThread*pWorkerThread2 = new QThread;
 CEyeProcWork *pWork2 = new CEyeProcWork();
 pWork2->Init(strSrcNameR, strMaskNameR, strIrisNameR);
 pWork2->moveToThread(pWorkerThread2);
 QObject::connect(pWorkerThread2, &QThread::started, pWork2, &CEyeProcWork::doWorking, Qt::QueuedConnection);
 pWorkerThread2->start();

问题就出在给工作对象添加了一个父类this指针。

Qt的多线程可以使用`QThread`类来实现,而`moveToThread`是一个很常用的函数,用于将一个QObject对象移动到另一个线程执行。 下面是一个使用`moveToThread`的简单例子: ```cpp #include <QCoreApplication> #include <QThread> #include <QDebug> class Worker : public QObject { Q_OBJECT public: Worker(QObject *parent = nullptr) : QObject(parent) {} public slots: void doWork() { qDebug() << "Worker thread:" << QThread::currentThreadId(); } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Worker worker; QThread thread; worker.moveToThread(&thread); QObject::connect(&thread, &QThread::started, &worker, &Worker::doWork); QObject::connect(&worker, &Worker::destroyed, &thread, &QThread::quit); thread.start(); return a.exec(); } ``` 在上面的例子,我们创建了一个`Worker`类,它继承自`QObject`类,并有一个`doWork`槽函数。我们将`worker`对象通过`moveToThread`函数移动到了`thread`线程。然后,我们通过`connect`函数将`thread`的`started`信号连接到`worker`的`doWork`槽函数上,当线程启动时,`doWork`槽函数会在`thread`线程执行。同时,我们还将`worker`的`destroyed`信号连接到`thread`的`quit`槽函数上,以保证线程能够正确退出。 需要注意的是,如果我们将一个QObject对象移动到了另一个线程执行,那么它的所有信号和槽函数都必须在该线程执行,否则会出现问题。所以,在上面的例子,我们将`worker`对象的`doWork`槽函数定义为`public slots`,并且在`thread`线程执行,以保证它能正确执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

设备系统软件集成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值