title: Creating a thread inside the class in C++
body: <p>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:</p>
  <p>In the class variable, <code>thread *t1</code> as if were to declare the class variable without initializing it.</p>
  <p>Then from the constructor, I did:</p>
  <pre><code>t1 = new Thread(functionName);
  </code></pre>
  <p>But, there's some error that I didn't get. I previously had simple working experience with threading in Java.</p>
  <p>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:</p>
  <pre><code>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,&quot;Score:0&quot;);
  
          ///right score board.
          drawRect(323,11,619,49,LIGHTMAGENTA);
          setfillstyle(SOLID_FILL,LIGHTMAGENTA);
          floodfill(330,40,LIGHTMAGENTA);
          setbkcolor(LIGHTMAGENTA);
          setcolor(LIGHTGREEN);
          outtextxy(350,20,&quot;Score:1&quot;);
      }
  
      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 &gt; 60)
                  {
                      drawLine(player1.x,player1.y0,player1.x,player1.y1,background);
                      player1.y0-=5;
                      player1.y1-=5;
                  }
              }
              else if(keyPressed == 's')
              {
                  if(player1.y1 &lt; 470)
                  {
                      drawLine(player1.x,player1.y0,player1.x,player1.y1,background);
                      player1.y0+=5;
                      player1.y1+=5;
                  }
              }
  
              if(keyPressed == 't')
              {
                  if(player2.y0 &gt; 60)
                  {
                      drawLine(player2.x,player2.y0,player2.x,player2.y1,background);
                      player2.y0-=5;
                      player2.y1-=5;
                  }
              }
              else if(keyPressed == 'g')
              {
                  if(player2.y1 &lt; 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&lt;&lt;&quot;Hello world&quot;&lt;&lt;endl;
          }
      ///-----------------------------------------------///
  
  
  public:
      game()
      {
          cleardevice();
          initializeData();
          setBackground();
          updateScoreBoard();
          playerBatPosition();
          ballPosition();
          startPlaying();
          t1 = new thread(startBallMovement);
      }
  };
  </code></pre>
  <p>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.</p>
  <p>Error:
  <a href="https://i.stack.imgur.com/vqnkz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vqnkz.png" alt="Error while compiling." /></a></p>