When do we use each of this function calls in a threaded application. given two functions fun1() and fun2() defined in the same class dealing with read/write of data into buffers(queue operation). to achieve multi-threading to these. we would have to run the two functions in a separate thread. now lets say the first function read is called at the start of its thread.
is it better to use moveTothread ( second thread)for function write at the start of the first functions thread
Or
define the second function in a new thread class and call that thread at the start of the first thread.
Like Piotr answered you should really have a look at the link he suggested.
As I understand your problem, that should solve your problem.
This is the simplified code from that blog:
class Producer
{
public:
Producer();
public slots:
void produce()
{ //do whatever to retrieve the data
//and then emit a produced signal with the data
emit produced(data);
//if no more data, emit a finished signal
emit finished();
}
signals:
void produced(QByteArray *data);
void finished();
};
class Consumer
{
public:
Consumer();
public slots:
void consume(QByteArray *data)
{
//process that data
//when finished processing emit a consumed signal
emit consumed();
//if no data left in queue emit finished
emit finished();
}
};
int main(...)
{
QCoreApplication app(...);
Producer producer;
Consumer consumer;
producer.connect(&consumer, SIGNAL(consumed()), SLOT(produce()));
consumer.connect(&producer, SIGNAL(produced(QByteArray *)), SLOT(consume(QByteArray *));
QThread producerThread;
QThread consumerThread;
producer.moveToThread(&producerThread);
consumer.moveToThread(&consumerThread);
//when producer thread is started, start to produce
producer.connect(&producerThread, SIGNAL(started()), SLOT(produce()));
//when consumer and producer are finished, stop the threads
consumerThread.connect(&consumer, SIGNAL(finished()), SLOT(quit()));
producerThread.connect(&producer, SIGNAL(finished()), SLOT(quit()));
producerThread.start();
consumerThread.start();
return app.exec();
}