Hello everyone ^_^

I need to know how to pause the execution of a code, without using sleep() or any sleep()-kind function

thats it...

thanks XD

It's another one of those "How do I do 'x' without using the obvious API for doing 'x'" type questions which some tutors seem to find amusing.

XD

I knew you would ask why...

It's for an assignment, and one of the requirements is to pause the execution without using sleep()

I knew you would ask why...

You could have answered the question you knew was coming before it came and saved me the trouble of asking. ;)

It's for an assignment, and one of the requirements is to pause the execution without using sleep()

That seems like kind of a pointless requirement... A way to pause the process already exists and I can't think of any legitimate reason not to use it. Assignments should teach you how to write programs, and ignoring the best option for a task isn't a good way to write programs.

Hello everyone ^_^

I need to know how to pause the execution of a code, without using sleep() or any sleep()-kind function

thats it...

thanks XD

In C++, you can do something like this

#include <iostream>
#include <limits>
 
int main() {
   
  // Rest of the code     
  
  //Clean the stream and ask for input
  std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
  std::cin.get();
  }

Does it matter how long the delay is? You can use time() to delay for a specific number of seconds. You need one time_t object and set it to the current time. Then enter a loop until the value returned by time() minus the original time equals or exceeds the desired delay number of seconds.

>> This may be a solution only for a very short delay, less than 5 milliseconds.

How is that ? It is a solution for any length of time. If you want more accuracy then other a different timer, such as the clock() function. It is not possible even with sleep() to get any more accurate. You could delay the program for hours and even days with my solution. True, sleep() is a much better solution because it doesn't take up so much processing time.

The idea is to keep the program innactive for 5 seconds.

You can use time() to delay for a specific number of seconds. You need one time_t object and set it to the current time. Then enter a loop until the value returned by time() minus the original time equals or exceeds the desired delay number of seconds.

If i do this, i think that it'll be running (Use cpu resources) but doing nothing...

>>If i do this, i think that it'll be running (Use cpu resources) but doing nothing...
That is correct -- and which is the reason everyone uses os specific sleep function. There is no other way to do it without consuming lots of cpu resources.

I'm not aware that portablity to other platforms is an issue in this thread. But if it is he can also use preprocessor directives to control which sleep is used

#if defined(_UNIX_)
usleep(1000); // sleep 1 second
#elif defined (_WIN32)
Sleep(1000);
#endif
This article has been dead for over six months. Start a new discussion instead.