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