C++ pause and resume while(true) loop

In summary: If so, use kbhit() to check for a pending key stroke, and if there is one, use getch() to get the character. If the character is 'p', call pasue(), if it's 'r', call resume(), and if it's 's', call stop().In summary, the conversation discusses the design of a class that can execute an infinite loop and be paused, resumed, and stopped by user input. The suggested solution involves using threads or available functions such as kbhit() and getch() to check for key strokes and call the appropriate functions.
  • #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

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
  • #2
Just start calculations in another thread with beginthread().

It will require some tricks, but AFAICT that's the only reasonable approach.
 
  • #3
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.
 
  • #4
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.
 
  • #5


I would suggest using a multi-threaded approach to address this problem. This means creating separate threads for the user input and the simulation loop, allowing them to run concurrently. This way, the user can input commands while the simulation is still running.

One way to implement this is by creating a listener thread that constantly listens for user input. This thread can then communicate with the simulation thread to pause, resume, or stop the loop. The simulation thread can also periodically check for any input from the listener thread and act accordingly.

In terms of design, you could create a class for the listener thread and another class for the simulation thread. These classes can communicate with each other through a shared data structure or by using synchronization techniques such as mutexes or semaphores.

It is important to carefully consider the synchronization and communication between threads to avoid any race conditions or deadlocks. You may also need to handle any potential errors or exceptions that may arise from the multi-threaded approach.

In summary, using multi-threading and careful design can allow for a user to pause, resume, and stop an infinite loop in a C++ program. However, it is important to thoroughly test and debug the implementation to ensure its reliability and stability.
 

Related to C++ pause and resume while(true) loop

1. How do I pause a while(true) loop in C++?

To pause a while(true) loop in C++, you can use the break statement. This will cause the loop to exit and execution will continue after the loop.

2. How do I resume a paused while(true) loop in C++?

To resume a paused while(true) loop in C++, you can use the continue statement. This will cause the loop to continue from the next iteration.

3. Can I pause and resume a while(true) loop multiple times in C++?

Yes, you can pause and resume a while(true) loop multiple times in C++. As long as the loop condition is still true, the loop will continue to execute.

4. What happens if I don't include a way to pause or exit a while(true) loop in C++?

If you do not include a way to pause or exit a while(true) loop in C++, it will continue to run indefinitely. This can lead to an infinite loop and your program may crash or hang.

5. How can I use a while(true) loop without causing an infinite loop in C++?

You can use a while(true) loop without causing an infinite loop in C++ by including a way to exit the loop, such as using the break statement or checking for a condition within the loop.

Similar threads

  • Programming and Computer Science
Replies
4
Views
760
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
715
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top