user7032676
user7032676

Reputation:

Creating a thread inside the class in C++

I am a beginner in threading with C++. Tried to create a thread t1 inside the class, but I want it not to be initialized. For this I did:

In the class variable, thread *t1 as if were to declare the class variable without initializing it.

Then from the constructor, I did:

t1 = new Thread(functionName);

But, there's some error that I didn't get. I previously had simple working experience with threading in Java.

Will this strategy cause an interlocking problem, assuming the solution exists to the way I want to implement it. (if I want to access the same variable without modifying it). Code goes like this:

class game:protected gameObjects
{
    thread *t1;
    ///-------------------- Variables-------------------///
    char keyPressed = NULL;
    /// structure for holding player information.
    struct player
    {
        int x, y0, y1,color;
    };
    player player1, player2;
    /// for ball
    int ballX, ballY, ballColor, ballBorderColor;

    ///--------------------------------------------------///
    void initializeData()
    {
        ballX = 42;
        ballY = 130;

        player1.color = LIGHTCYAN;
        player1.x = 30;
        player1.y0 = 100;
        player1.y1 = 200;


        player2.color = LIGHTMAGENTA;
        player2.x = 600;
        player2.y0 = 100;
        player2.y1 = 200;

        ///  Basic setUp For Game and Game Screen
        ballBorderColor = RED;
        ballColor  = GREEN;
    }

    void updateScoreBoard()
    {

        /// whole score board border.
        drawRect(20,10,620,50,RED);

        /// left score board
        drawRect(21,11,321,49,CYAN);
        setfillstyle(SOLID_FILL,CYAN);
        floodfill(30,40,CYAN);
        setbkcolor(CYAN);
        setcolor(LIGHTGREEN);
        outtextxy(35,20,"Score:0");

        ///right score board.
        drawRect(323,11,619,49,LIGHTMAGENTA);
        setfillstyle(SOLID_FILL,LIGHTMAGENTA);
        floodfill(330,40,LIGHTMAGENTA);
        setbkcolor(LIGHTMAGENTA);
        setcolor(LIGHTGREEN);
        outtextxy(350,20,"Score:1");
    }

    void ballPosition()
    {
        setfillstyle(SOLID_FILL,ballColor);
        drawCircle(ballX,ballY,10,ballBorderColor); ///ball for game
        floodfill(ballX,ballY,ballBorderColor);
    }

    void playerBatPosition()
    {
        drawLine(player1.x,player1.y0,player1.x,player1.y1,player1.color);
        drawLine(player2.x,player2.y0,player2.x,player2.y1,player2.color);
    }


    void setBackground()
    {
        setfillstyle(SOLID_FILL,background);
        drawRect(0,0,getmaxx(),getmaxy(),RED);
        floodfill(getmaxx()/2,getmaxy()/2,RED);

        drawRect(20,60,620,470,WHITE); ///white line.
    }

    void updateScreenActivity()
    {
        playerBatPosition();
    }


    void startPlaying()
    {
        do
        {
            keyPressed = _getch();
            if(keyPressed == 'w')
            {
                if(player1.y0 > 60)
                {
                    drawLine(player1.x,player1.y0,player1.x,player1.y1,background);
                    player1.y0-=5;
                    player1.y1-=5;
                }
            }
            else if(keyPressed == 's')
            {
                if(player1.y1 < 470)
                {
                    drawLine(player1.x,player1.y0,player1.x,player1.y1,background);
                    player1.y0+=5;
                    player1.y1+=5;
                }
            }

            if(keyPressed == 't')
            {
                if(player2.y0 > 60)
                {
                    drawLine(player2.x,player2.y0,player2.x,player2.y1,background);
                    player2.y0-=5;
                    player2.y1-=5;
                }
            }
            else if(keyPressed == 'g')
            {
                if(player2.y1 < 470)
                {
                    drawLine(player2.x,player2.y0,player2.x,player2.y1,background);
                    player2.y0+=5;
                    player2.y1+=5;
                }
            }
            updateScreenActivity();
        }
        while(keyPressed != 'q');
    }


     ///-------------------Threading call --------------///

        void startBallMovement(){
            cout<<"Hello world"<<endl;
        }
    ///-----------------------------------------------///


public:
    game()
    {
        cleardevice();
        initializeData();
        setBackground();
        updateScoreBoard();
        playerBatPosition();
        ballPosition();
        startPlaying();
        t1 = new thread(startBallMovement);
    }
};

What I want to do is create a movement of a circle in different paths from the thread. I might sometimes need to access the variables from the thread to simulate the movement in different directions as per the user's strategy.

Error: Error while compiling.

Upvotes: 2

Views: 5114

Answers (1)

ObliteratedJillo
ObliteratedJillo

Reputation: 5166

Your class implementation looks a bit lousy. But try something like this.

class game
{
    private: thread* t;

    public:
    game()
    {
        t =  new thread([this]() {startBallMovement();} );
    }

    ~game()
    {
        if(t != nullptr)
        {
            t->join();
            delete t;
        }
    }

    void startBallMovement()
    {
    }

};

Upvotes: 3

Related Questions