Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I got thread problem
QtWS25 Last Chance

I got thread problem

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 2.6k Views
  • 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 Offline
    K Offline
    karim24
    wrote on last edited by
    #1

    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
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Hi, what do you mean by

      bq. it prints numbers with no meanning

      1 Reply Last reply
      0
      • K Offline
        K Offline
        karim24
        wrote on last edited by
        #3

        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
        0
        • D Offline
          D Offline
          dbzhang800
          wrote on last edited by
          #4

          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
          0
          • K Offline
            K Offline
            karim24
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MichelS
              wrote on last edited by
              #6

              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
              0
              • D Offline
                D Offline
                dbzhang800
                wrote on last edited by
                #7

                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
                0
                • JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  [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
                  0
                  • K Offline
                    K Offline
                    karim24
                    wrote on last edited by
                    #9

                    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
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved