Pustovalov Dmitry
Pustovalov Dmitry

Reputation: 1047

Proper way to run managable background thread with QThread

I need to run few background threads which must be managable in a way that I can safely stop it anytime. Threads should do some repetable task.

I read documentation and the best way which I can find is to subclass QThread and reimplement run() method:

class BackgroundThread: public QThread
{
   Q_OBJECT

   virtual void run() Q_DECL_OVERRIDE
   {
       while (true)
       {
          // do some routine task
          // sleep ...
       }  
   }
};

I like this because I can run code in separate thread and I don't need to do unconvient Qt magic with moveToThread and connecting up to 10 signals/slots to properly manage thread resources.

The problem is that I can't find a way to safely stop the thread. I don't want to terminate it in a random place of execution, I would want it to stop when next iteration ends. The only way to achive it which I see now is to add some atomic flag to thread class and set it from main thread when I need to stop it, but I really don't like this solution.

What is the best way to implement managable background thread using Qt5?

Upvotes: 0

Views: 1678

Answers (1)

ixSci
ixSci

Reputation: 13708

You don't need any magic and "10 signals/slots". Just create your worker:

class Worker: public QObject
{
    ...
public slots:
    void routineTask();
}

Somewhere in your code:

QThread bckgThread;
bckgThread.start();
Worker worker;
worker.moveToThread(&bckgThread);

Connect some signal to the routineTask slot to call it or use QMetaObject::invokeMethod. And when you are done with the thread, just call:

bckgThread.quit();
bckgThread.wait();

That's pretty simple pattern. Why go the hard way and subclass QThread?

Upvotes: 1

Related Questions