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. Bad multithreading
Forum Updated to NodeBB v4.3 + New Features

Bad multithreading

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 2.2k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    Mikee
    wrote on last edited by
    #1

    Hi.
    It works well

    QVector<double> Perebor; 
            for (double i=5;i<100;i=i+3)
            {
                Perebor.append(i);
            }
            qDebug()<<Perebor;
            QFuture<void> Perebor2 = QtConcurrent::map(Perebor,[&w](const double& d){ w.StrategyCod(d); });
    

    It works badly

    w.MultiThreading=true; 
      //-----------------------------------------------------------------------
       if (w.MultiThreading==true)
       {
           QVector<double> Perebor; 
           for (double i=5;i<100;i=i+3)
           {
               Perebor.append(i);
           }
           //qDebug()<<Perebor;
           QFuture<void> Perebor2 = QtConcurrent::map(Perebor,[&w](const double& d){ w.StrategyCod(d); }); 
       }
    
    
    void MainWindow::StrategyCod(double Parametr)
    {
       double i=Parametr;
       qDebug()<<i;
    }
    

    return: 2.122e-314,0,0,0,0,0,0,0,0,1.16149e+260,0,1.15973e+260,4.94066e-324,0,0,0,0,0,0,0,0,0,0,3.31024e-322,0,0,0,0,0,0,0,0
    Why does not this code?

    K 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by VRonin
      #2

      Hi
      Where is the "works well" located ?
      in main.cpp ?

      i think in "works badly" , you variable runs out of scope and get deleted

      if (w.MultiThreading==true) {
      QFuture<void> Perebor2 = QtConcurrent::map(Perebor,[&w](const double& d){
      w.StrategyCod(d); });
      } ///Perebor dies here.
      ....

      1 Reply Last reply
      6
      • M Mikee

        Hi.
        It works well

        QVector<double> Perebor; 
                for (double i=5;i<100;i=i+3)
                {
                    Perebor.append(i);
                }
                qDebug()<<Perebor;
                QFuture<void> Perebor2 = QtConcurrent::map(Perebor,[&w](const double& d){ w.StrategyCod(d); });
        

        It works badly

        w.MultiThreading=true; 
          //-----------------------------------------------------------------------
           if (w.MultiThreading==true)
           {
               QVector<double> Perebor; 
               for (double i=5;i<100;i=i+3)
               {
                   Perebor.append(i);
               }
               //qDebug()<<Perebor;
               QFuture<void> Perebor2 = QtConcurrent::map(Perebor,[&w](const double& d){ w.StrategyCod(d); }); 
           }
        
        
        void MainWindow::StrategyCod(double Parametr)
        {
           double i=Parametr;
           qDebug()<<i;
        }
        

        return: 2.122e-314,0,0,0,0,0,0,0,0,1.16149e+260,0,1.15973e+260,4.94066e-324,0,0,0,0,0,0,0,0,0,0,3.31024e-322,0,0,0,0,0,0,0,0
        Why does not this code?

        K Offline
        K Offline
        koahnig
        wrote on last edited by koahnig
        #3

        @Mikee said in Bad multithreading:

        if (w.MultiThreading==true)
        {
        QVector<double> Perebor;
        for (double i=5;i<100;i=i+3)
        {
        Perebor.append(i);
        }
        //qDebug()<<Perebor;
        QFuture<void> Perebor2 = QtConcurrent::map(Perebor,[&w](const double& d){ w.StrategyCod(d); });
        }

        QVector<double> Perebor; will be destroyed after directly after you start QtConcurrent::map(Perebor, ...

        You do not show what happens with the vector. Possibly it stays, because you do not loose focus.

        [edit: koahnig] Already mentioned by @mrjj

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        4
        • M Offline
          M Offline
          Mikee
          wrote on last edited by Mikee
          #4

          Why destroyed QVector<double> Perebor; ?
          Code in main.cpp

          jsulmJ 1 Reply Last reply
          0
          • M Mikee

            Why destroyed QVector<double> Perebor; ?
            Code in main.cpp

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Mikee said in Bad multithreading:

            Why destroyed QVector<double> Perebor; ?

            Because it is in a scope:

            if (w.MultiThreading==true)
            {
            QVector<double> Perebor; // Perebor does only exist in this if block
            for (double i=5;i<100;i=i+3)
            {
            

            You should read about C/C++ scopes, it is not related to Qt.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            4
            • M Offline
              M Offline
              Mikee
              wrote on last edited by
              #6

              thank you

              K 1 Reply Last reply
              1
              • M Mikee

                thank you

                K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                @Mikee

                To add to @jsulm it is not a Qt related problem. It is a multi-threading issue here.

                void foo() 
                {
                     if ( true) 
                    {
                          int i = 10;
                    }
                    int j = i;
                }
                

                This will give you a compiling error and it is very obvious with scopes.

                void assign_sometimes_later ( int & i )
                {
                     int j = i;
                }
                void foo() 
                {
                     if ( true) 
                    {
                          int i = 10;
                          assign_sometimes_later (i);
                    }
                }
                

                This will compile, but the compiler cannot see when the routine is being executed. The problem is the reference in the calling list of the routine.

                Vote the answer(s) that helped you to solve your issue(s)

                JonBJ 1 Reply Last reply
                1
                • K koahnig

                  @Mikee

                  To add to @jsulm it is not a Qt related problem. It is a multi-threading issue here.

                  void foo() 
                  {
                       if ( true) 
                      {
                            int i = 10;
                      }
                      int j = i;
                  }
                  

                  This will give you a compiling error and it is very obvious with scopes.

                  void assign_sometimes_later ( int & i )
                  {
                       int j = i;
                  }
                  void foo() 
                  {
                       if ( true) 
                      {
                            int i = 10;
                            assign_sometimes_later (i);
                      }
                  }
                  

                  This will compile, but the compiler cannot see when the routine is being executed. The problem is the reference in the calling list of the routine.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @koahnig
                  For the record, I don't see what you are saying would go wrong with your 2nd example code --- seems to me it would work OK?

                  K 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @koahnig
                    For the record, I don't see what you are saying would go wrong with your 2nd example code --- seems to me it would work OK?

                    K Offline
                    K Offline
                    koahnig
                    wrote on last edited by
                    #9

                    @JNBarchan

                    The second is basically what the compiler sees. It is completely valid, but when you assume the routine is executed sometimes later through an imaginary multi-threading call, it would fail in execution.

                    Therefore, this part is given to highlight the misery happened in the original example and why it was not detected.

                    Vote the answer(s) that helped you to solve your issue(s)

                    1 Reply Last reply
                    1

                    • Login

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