- #1
guest1234
- 41
- 1
How to design a class, that executes an infinite loop, but may be paused, resumed and stopped by user input? I'll give you an example
Now obviously s.start() takes up the whole program and there's now way of stopping it properly. It just doesn't have the chance to get user input.
What is the right way to do it? I assume that threads are needed but I have yet no idea how to use them here. Maybe creating a listener class and putting this and model to separate threads? Any help appreciated.
Code:
class Simulation {
public:
void start(){
m.init();
while(!m.isFinished())
m.update(); // computationally expensive calculation
}
void pause();
void resume();
void stop();
private:
Model m;
};
int main() {
Simulation s;
s.start();
// wait for user input
}
Now obviously s.start() takes up the whole program and there's now way of stopping it properly. It just doesn't have the chance to get user input.
What is the right way to do it? I assume that threads are needed but I have yet no idea how to use them here. Maybe creating a listener class and putting this and model to separate threads? Any help appreciated.
Last edited: