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

Re: [Linux] Need a way to pause a program without using sleep 80 80

Why can't you use a sleep() kind function?

Re: [Linux] Need a way to pause a program without using sleep 80 80

select... pauses any way you want

Re: [Linux] Need a way to pause a program without using sleep 80 80

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.

Re: [Linux] Need a way to pause a program without using sleep 80 80

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()

Re: [Linux] Need a way to pause a program without using sleep 80 80

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.

Re: [Linux] Need a way to pause a program without using sleep 80 80

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();
  }
Re: [Linux] Need a way to pause a program without using sleep 80 80
Re: [Linux] Need a way to pause a program without using sleep 80 80

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.

Re: [Linux] Need a way to pause a program without using sleep 80 80

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

Re: [Linux] Need a way to pause a program without using sleep 80 80

>> 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.

Re: [Linux] Need a way to pause a program without using sleep 80 80

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...

Re: [Linux] Need a way to pause a program without using sleep 80 80

>>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.

Re: [Linux] Need a way to pause a program without using sleep 80 80

At least one sleep function is cross-platform -- g_usleep in glib.

Re: [Linux] Need a way to pause a program without using sleep 80 80

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.