C++ pause and resume while(true) loop

  • Context: C/C++ 
  • Thread starter Thread starter guest1234
  • Start date Start date
  • Tags Tags
    C++ Loop Resume
Click For Summary

Discussion Overview

The discussion revolves around designing a C++ class that can execute an infinite loop while allowing for user input to pause, resume, or stop the execution. The scope includes technical implementation details, threading considerations, and user input handling.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests creating a separate thread for calculations using beginthread() to allow for user input handling.
  • Another participant notes that the solution may depend on the environment, indicating that if threads are available, one could run the infinite loop in one thread and handle IO in another.
  • It is mentioned that if threads are not available, alternatives like "signals" or "messages" that need to be read periodically could be used.
  • A participant proposes checking if the compiler or operating system supports a function like kbhit() to detect pending key strokes.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a single solution, as multiple approaches are suggested depending on the availability of threads and specific environment constraints.

Contextual Notes

The discussion highlights limitations based on the environment, such as the availability of threading capabilities and specific functions for handling user input.

guest1234
Messages
38
Reaction score
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

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:
Technology news on Phys.org
Just start calculations in another thread with beginthread().

It will require some tricks, but AFAICT that's the only reasonable approach.
 
The answer to this problem depends on your environment. If threads are available, then you could use one thread to run the infinite loop and the other to process IO.

If threads are not available, you may have "signals". Or you may have "messages" which you have to read periodically.

Unless you specify more details, there is no definite answer.
 
See if your compiler / operating system supports a function like kbhit() which returns a status to indicate if there's a pending key stroke or not.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
4
Views
3K
  • · Replies 14 ·
Replies
14
Views
35K
Replies
3
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K