Varun Chitre
Varun Chitre

Reputation: 3220

Multithreading in Qt - doubts and problems

nearly searching for hours I became more confused about the Multithreading concept and I need help understanding and implementing it. After searching I have come to the following implementation

main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"
#include <mythread1.h>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
mythread abc; 
abc.start();
abc.wait();
return a.exec();
}

In above code mythread.h is my header file which I created for multithreading and below is my code of mythread1.h

mythread1.h

#ifndef MYTHREAD1_H
#define MYTHREAD1_H
#include <QtGui>
#include <windows.h>

class mythread : public QThread
{
public:
    void run();
};

void mythread::run()
{

}



#endif // MYTHREAD_H

Now my questions are

  1. I have defined only one function run() and when the thread is initialized how compiler determines it has to execute run() function and what it does when it has multiple functions, I got this question because in main.cpp I just typed abc.start(); and didn't specify what to execute?
  2. Inside mythread1.h and in run function I made a infinite loop using while(0<1) and ran the program and to my surprise I was just presented with white screen with 100% CPU usage(not a surprise), but it should run concurrently without interfering with main thread right? then why this is happening?
  3. I then used QFile::copy("pathtomytargetfile","targetpath"); inside the run function inside in mythread1.cpp but that didn't work :O and it didn't copy that file but when i connected it with Push button in main thread then it got successfully copied, why this happened?
  4. Does anyone know a simpler way to implement multithreading?

Thank you

Upvotes: 1

Views: 352

Answers (2)

bjoernz
bjoernz

Reputation: 3852

My answer might confuse you even more, but the following links are worth reading in my opinion:

  1. This is an article by the engineer who introduced the QThread class. He apologizes for suggesting that inheriting from QThread is the way to go.
  2. This article shows you, how you should do it (strictly speaking -- inheriting from QThread will work as well, it just is not as nice design-wise).
  3. On this page you can find an overview of multithreading techniques that Qt offers and some help deciding which one you should use for your specific problem.

HTH

Upvotes: 1

Kamil Klimek
Kamil Klimek

Reputation: 13130

First of all: run method should be protected!

Secondly, what do you mean by "I have defined only one function run()". You defined method, not function. abc.start() means that abc instance of mythread will start and use it's entry point method void run(). You can't declare more than one method with same signature. And if you wonder how it calls your run() instead of QThread::run() then you need to read something about virtual methods in C++

Ad. 3: How can we tell why your file didn't copy? Check what errors QFile provides you. Change your code something like that:

QFile f("pathtomytargetfile");
if( ! f.copy("targetpath") )
{
     qDebug() << f.errorString();
}

It will provide you some usefull info

Upvotes: 1

Related Questions