I got thread problem



  • the programm suppose to print 0->100 using 3 thread
    i can't find the problem, in here it runs but it prints numbers with no meanning

    #include <QCoreApplication>
    #include<mythread.h>
    #include<QThread>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    MyThread mt1,mt2,mt3;
    mt1.start();
    mt2.start();
    mt3.start();
    return a.exec();
    }

    #ifndef MYTHREAD_H
    #define MYTHREAD_H
    #include<QThread>
    #include<QtCore>

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

    #endif // MYTHREAD_H

    mythead.cpp:
    #include "mythread.h"
    #include<QThread>
    #include<QDebug>
    #include<QtCore>
    MyThread::MyThread()
    {
    }
    void MyThread:: run()
    {
    //qDebug()<<this->name<<"running hahah...";
    for(int i=0;i<100;i++)
    {
    qDebug()<<i;
    }
    }



  • Hi, what do you mean by

    bq. it prints numbers with no meanning



  • i mean like that not actually no meanning but not what it's supposed to be
    0
    1
    2

    33
    45
    6
    7
    11



  • Can you tell us which results are you expected?

    The output looks good for me.
    [quote author="karim24" date="1372386765"]i mean like that not actually no meanning but not what it's supposed to be
    0
    1
    2

    33
    45
    6
    7
    11[/quote]



  • I am expecting
    0
    1
    2
    3
    4
    5
    6
    .
    .
    .
    99



  • If you have multiple threads, it is the expected behavior to have those threads running simultaneously. So the output will almost never look like 0,1,2,3,...



  • You want to make the three functions run one after another, instead of concurrent.So, thread isn't what you are looking for.
    [quote author="karim24" date="1372421914"]I am expecting
    0
    1
    2
    3
    4
    5
    6
    .
    .
    .
    99[/quote]


  • Moderators

    [quote author="karim24" date="1372421914"]I am expecting
    0
    1
    2
    3
    4
    5
    6
    .
    .
    .
    99[/quote]There is no good way to do that using threads. Just write
    @
    int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    for (int i = 0; i < 100; ++i)
        qDebug() << i;
    
    return a.exec&#40;&#41;;
    

    }
    @

    [quote author="karim24" date="1372385882"]
    void MyThread:: run() {
    for(int i=0;i<100;i++) { qDebug()<<i; }
    }
    [/quote]If you create 3 of these threads, you will get 300 numbers. If you are "lucky", you will see:

    0
    0
    0
    1
    1
    1
    2
    2
    2
    ...

    But, as you have discovered in your example, the numbers will come out messed up because the threads will NOT wait for each other.

    What do you plan to do with threads?



  • basically nothing in the meanwhile ,i am following a tutorial about qt in youtube , and he discussed threads in five or six videos ,and worked fine for him
    http://www.youtube.com/watch?v=JaGqGhRW5Ks&list=SP2D1942A4688E9D63 .


Log in to reply
 

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