mardi 5 décembre 2017

using thread and pointer and passing pointers to thread functions

i've spent quiet a time sorting it out but i am stuck. Please read the code and take the meaning of every function literally. assume all functions to operate successfully on their on.

void thrperball(ball** B,int& n)
{
    if(indi%8!=n)
    {
       return;
    }
//check if the ball is colliding with each other;
    int i;
//assume their are 8 balls
    for(i=0;(i!=n && i<8);i++)
    {
        if(isnear(*(B[n]),*(B[i])))
        {
            int c;
            for(c=0;c<10;c++)
            {
               if(isnear(*(B[n]),*(B[i]),2.05))
               {
                    colball( *(B[n]),*(B[i]) );
                    break;
                }
                B[i]->move(0.0025);
                B[n]->move(0.0025);
            }
            B[i]->move( (10-c)*0.0025 );
            B[n]->move( (10-c)*0.0025 );
            indi+=2;
        }
    }
    if(i==8 || (i==7 && n==7))
    {
       indi++;
       colormove(*(B[n]));
    }
    if(n==7)
       Time+=0.025;
    return;
 }

int main()
{    
   ball** B=new ball*[8];
    for(int i=0;i<8;i++)
        B[i]=new ball;
    B[0]->change(16,16,0,20);
    B[1]->change(2,2,20,0);
    B[2]->change(4,4,30,0);
    B[3]->change(6,6,25,0);
    B[4]->change(8,8,20,0);
    B[5]->change(10,10,0,20);
    B[6]->change(12,12,0,25);
    B[7]->change(14,14,0,30);
    cout << "task1 successful\n";
    thread thr[8] ;
    for( int i=0 ; i<8 ; i++)
       thr[i]=thread(&thrperball,ref(B),ref(i));
    for( int i=0 ; i<8 ; i++ )
        thr[i].join() ;
    display(B) ;
    return 0;
}

forget about indi variable. when the display function is executed it gives the output::

     task1 successful
     0       16,16   2,2     4,4     6,6     8,8     10,10   12,12   14,14

is there a problem in passing of variables in the thread?

Aucun commentaire:

Enregistrer un commentaire