C/C++ C++ pause and resume while(true) loop

  • Thread starter Thread starter guest1234
  • Start date Start date
  • Tags Tags
    C++ Loop Resume
Click For Summary
To design a class that executes an infinite loop while allowing for user input to pause, resume, or stop the process, a multithreaded approach is essential. The main class, such as Simulation, should run its computationally intensive tasks in a separate thread, enabling the main thread to handle user input without interruption. This can be achieved using threading functions like beginthread(). If threading is not an option, alternative methods such as signals or periodic message checks may be employed. Additionally, utilizing functions like kbhit() can help detect user input without blocking the main execution flow. The specific implementation will depend on the capabilities of the compiler and operating system in use.
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

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