Bad multithreading



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


  • Qt Champions 2016

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


  • Moderators

    @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



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


  • Moderators

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



  • thank you


  • Moderators

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



  • @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?


  • Moderators

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


Log in to reply
 

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