Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    I got thread problem

    General and Desktop
    4
    9
    2159
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      karim24 last edited by

      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;
      }
      }

      1 Reply Last reply Reply Quote 0
      • D
        dbzhang800 last edited by

        Hi, what do you mean by

        bq. it prints numbers with no meanning

        1 Reply Last reply Reply Quote 0
        • K
          karim24 last edited by

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

          33
          45
          6
          7
          11

          1 Reply Last reply Reply Quote 0
          • D
            dbzhang800 last edited by

            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]

            1 Reply Last reply Reply Quote 0
            • K
              karim24 last edited by

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

              1 Reply Last reply Reply Quote 0
              • M
                MichelS last edited by

                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,...

                1 Reply Last reply Reply Quote 0
                • D
                  dbzhang800 last edited by

                  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]

                  1 Reply Last reply Reply Quote 0
                  • JKSH
                    JKSH Moderators last edited by

                    [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?

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply Reply Quote 0
                    • K
                      karim24 last edited by

                      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 .

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post