0

First of all, forgive me if there’s an obvious solution, I’m completely new to the use of thread (and only know a bit about the theory).

I’m currently developing an application in which I want to use threads. Basically, it retrieves data an displays it on a graph in real time with Qt.

I’ve followed the guidelines of this post on how to use correctly QThread, but then I’m concerned with a few details.

Basically, I want to implement a pause, that will stop the real-time query and update, which can then be resumed later on.

At the moment, my class looks like this :

class DataProcessor : public QObject{
    Q_OBJECT

public:
    DataProcessor();
    ~DataProcessor();

//Internal details

public slots:
    //mapped to the started() signal of a QThread
    void process();

signals:
    void finished();
    void error(QString error);
}

I wonder how I could implement a pause feature. After reading Bruce Dawson’s post In praise of Idleness, I’ve noted a few methods that I should avoid (most importantly, avoiding busy wait)

Then, it doesn’t quite fit my needs, as Bruce talks about waiting for another thread to exit its critical section. Here, I just want to pause the work done by the thread’s worker on user input, and restart it the same way.

My question is: how can I implement an efficient pause for my threaded code ?

I’ve thought of using a sort of flag in my worker, that could be set from the main (something like bool m_paused;), but once set, I don’t know how I can actually pause efficiently my worker.

Note: This question was not written by me, but it describes my problem exactly. Source: link

mario.schlipf
  • 1,257
  • 2
  • 13
  • 29
  • 1
    Use QWaitCondition in process method where it is safely to pause execution. – Dmitry Sazonov Jul 16 '13 at 16:13
  • As you probably already know, a lot of people will discourage inheriting from QThread. However, it seems like this is one case where you'll need to [inherit](http://stackoverflow.com/questions/9075837/pause-and-resume-a-qthread). I haven't implemented this myself, just found it in a search – Son-Huy Pham Jul 16 '13 at 17:38
  • @DmitrySazonov can you give an example? – mario.schlipf Jul 16 '13 at 21:33
  • @Huytard Yes, I read that inheriting from QThread is a bad idea. At the moment I try to avoid it, but if this is the only (/better) solution, you may give me an example :) – mario.schlipf Jul 16 '13 at 21:34
  • 1
    I posted a link on (probably hard to see) http://stackoverflow.com/questions/9075837/pause-and-resume-a-qthread. This form of inheritance actually doesn't violate the whole "don't over-ride run()" – Son-Huy Pham Jul 16 '13 at 21:43
  • 1
    set up an object handling your code, move it the thread, pause (stop, whatever) your code on signalXYZ(). You still won't need to subclass. Moreover you can implement QWaitCondition to ensure something has been changed in another thread. – Sebastian Lange Jul 17 '13 at 06:53

0 Answers0