简体   繁体   中英

How to manage QThread in Qt c++?

I am trying to learn QThread in Qt. I wrote following code for QThread which is working quite good.

    QThread* mThread = new QThread;
    FaceCutThread* mFaceCut = new FaceCutThread();
    mFaceCut->moveToThread(mThread);

    connect(mThread, SIGNAL(finished()), mFaceCut, SLOT(deleteLater()));
    connect(this, SIGNAL(operateFaceCut(std::string)), mFaceCut, SLOT(processFaceCut(std::string)));
    connect(mFaceCut, SIGNAL(isFinisedFaceCut(QImage,bool)), this, SLOT(handleFaceCutResults(QImage,bool)));
    mThread->start();

Now I want to add one more class which should work as a QThread, like;

Enroll *mEnroll = new Enroll();

Should I use mEnroll object with previous mThread or should I create new mThread2;

QThread* mThread2 = new QThread;
mEnroll->moveToThread(mThread2);

What's the advantages and disadvantages?

It depends what do you want to achieve. If you put the same classes in the same thread then they will be executed in the same thread. If you want them to be executed in separate threads then put to another thread. For example if you want to use them independently - one thread loading/reading data, second processing data and connecting them via signals. Separate threads creates problem with synchronization if threads shares resources etc. ( long topic http://www.drdobbs.com/tools/avoiding-classic-threading-problems/231000499 ) Having objects in the same thread means that objects have common

  • Register state (including PC and stack pointer)

  • Stack

  • Signal mask

  • Priority

  • Thread-private storage

And this can be treated as advantage or disadvantage ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM