Question on how to do threading

Hi, I am new to C++ and this website.

-- (Skip my rant and read the next paragraph) --
I am coming here because I need help and I am so fed up with Stack Overflow. Apparently, I was banned from asking questions because "they were not good enough questions" which is rediculous saying that the whole point of the site is to get answers to your questions. ARRG!

Enough with my little rant, sorry about that. I wrote some code that I do not understand and it doesn't really work... What I am trying to do is make a game loop for a console type of game, so I was thinking I would have to put it on a separate thread if I want to ever be able to stop it (since it is nearly like an infinite loop). I get the feeling I am breaking a ton of rules of writing "good code" here so please help me out.

Here's the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void gameLoop() {
  while(game.getState()) {
    cout << "Tick" << endl;
  }
}

int main() {
  
  mh.message("Getting original game state...");
  cout << "Game State: " << game.getState() << endl;
  
  game.start();
  thread gLoop(gameLoop);
  gLoop.join();
  
  
  game.stop();
  
  return 0;
}


Thanks,
Joel
joeldesante wrote:
the whole point of the site is to get answers to your questions. ARRG!

not *your* questions, specifically. The point is to build a knowledge base where you can find answers to your questions because someone already asked them in a way that is useful to most programmers. There are detailed instructions on how to ask a new question that would help StackOverflow's mission.

Case in point, your code would not be acceptable because it is not the smallest complete program that reproduces the problem you've experienced and it's unclear what actual problem you experienced: "doesn't really work" is not a description another programmer can help with since we don't know what kind of "work" was expected.

But, since this is a discussion forum rather than a Q&A knowledge database, it is indeed a better place for this discussion.

responding specifically to
I was thinking I would have to put it on a separate thread if I want to ever be able to stop it

If you want to stop the loop, just exit it, with a break statement or have your game.gameState() become false, when the exit event occurs in-game. Threads give you concurrency, not "ability to stop", and what is your main thread doing concurrent with your event loop thread? Nothing, sitting in gLoop.join(), wasting a chunk of memory.
Sorry I was unclear. I am very new to C++. What I want to do is make a game loop. So far the way I have it set up is that game.stop() turns isRunning to false. Unfortunately, once the loop is going I do not see a way of stopping it unless it was on another thread. I only have a basic knowledge of multi threaded programs. But when the game loop starts it will not stop. That I guess would be my issue.
But when the game loop starts it will not stop.

In your code sample, the loop is
1
2
3
  while(game.getState()) {
    cout << "Tick" << endl;
  }

This loop stops when game.getState() returns false. As written, the loop does nothing (other than endless printing), so indeed it won't stop, If you fill it with actual content, like game event handling, that code would change the state of the game. Something like
1
2
3
4
5
6
7
8
  while(game.getState()) {
    auto action = getUserAction();
    switch(action) {
        case MOVE : game.doMove(); break;
        case SAVE : game.doSave(); break;
        case QUIT : game.isRunning = false; // I assume getState returns that?
     }
  }


I do not see a way of stopping it unless it was on another thread

How do you think being on another thread helps in stopping it?
Topic archived. No new replies allowed.