在c ++中

时间:2018-02-20 04:45:04

标签: c++ multithreading

我是初学者用c ++进行线程化。试图在类中创建一个线程t1,但我希望它不被初始化。为此,我做了:

在类变量中。 thread * t1 好像是在没有初始化的情况下声明类变量。

然后从我做的构造函数。
t1 = new Thread(functionName);

但是我有一些错误。我以前在java中使用线程的简单工作经验。

假设解决方案存在于我想要实现的方式,此策略是否会导致互锁问题。 (如果我想在不修改的情况下访问同一个变量)。代码如下:

 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);
    }
};

我想要做的是在线程的不同路径中创建一个圆圈运动。我可能有时需要从线程中访问变量,以根据用户的策略模拟不同方向的移动。谢谢!!

错误: Error while compiling.

1 个答案:

答案 0 :(得分:1)

你的类实现看起来有点糟糕。但尝试这样的事情。

class game
{
    private: thread* t;

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

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

    void startBallMovement()
    {
    }

};