[SOLVED] QThread is not working

  • Hello. I think I have a problem but I am not sure. I have made a simple example for QThread, and the problem is that output of the program is not whatI have expected. I have expected output like this:
    but I get output like this:

    Please what am I doing wrong.

    This is my code:
    @#include <QtCore/QCoreApplication>
    #include "mthread.h"

    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    mThread thread1;
    mThread thread2;
    mThread thread3;
    return a.exec&#40;&#41;;


    @#ifndef MTHREAD_H
    #define MTHREAD_H

    #include <QtCore>

    class mThread : public QThread
    void run();

    #endif // MTHREAD_H

    @#include "mthread.h"


    void mThread::run()
    { for (int f1=0; f1<10; f1++)

  • Your code is working perfectly. It is just not doing what you were expecting. In this case, it is time to adjust your expectations.

    There is no way to predict at what moment one thread yields control to another thread. It may happen at any moment, or not at all. They may simply run really parallel on different cores or processors. So, why do you expect a certain order of output? Did you take into acount that perhaps your loops are so fast, that control is simply not yet yielded by the time your thread finishes? I would be worried if a tread was yieded so fast and so often as to not be able to run a small loop like this, as context changes are expensive!

  • By modifying your code from main.cpp to this:

    @int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    mThread thread1, thread2, thread3;
    return a.exec(&#41;;


    I get the following output:

    0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7...

    Even thou stack allocation is amazingly fast, an empty for loop is quite fast too. By creating the three threads before running them I get more concurent behavior, which goes to show that the overhead of creating a thread is too much for your scenario to get concurrent execution of your run() method.

    Creating a thread takes longer than a for loop to count to 10, as simple as that, even considering you use post increment which is suboptimal.

  • Thank you very much for your answers.

    I am learning Qt from voidrealms.com videos, and when I watched videos about QThread Bryan in his example got output of program like this 1,1,2,1,2,3,2,3,4,3,4,5,4,… so when I tried the example I didnt get that random order but 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, so I thought I did something wrong.

    Thank you Andre and ddriver, for explanation, and for helping me to learn more about Qt. ;-)

  • Unless to sync your threads all you can expect is the unexpected, it is completely normal to get different output every time. The "seemingly" perfect output I got when I tested your code was purely by coincidence, by adding a name variable for every thread the ugly truth behind multithreading is revealed:


    It is the operating system thread scheduler that manages running threads, and besides setting priority there is not much you can do, unless you sync your threads.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.