-3
void PauseFor( int seconds)
{
    clock_t temp;
    temp = clock () + seconds * CLOCKS_PER_SEC ;
    while (clock() < temp) {}

}

Ok, this is my function. I want to make stop executing code for 2 seconds in this time do nothing.

Example:

  1. RUN FUNCTION
  2. after firs run , pause 2 seconds.
  3. In this time 2 seconds do nothing.
  4. after 2 seconds, you can run function again.

What i want to to ? I want to stop using function for 2 seconds, In this time 2 seconds nothing happen..

Thanks :)

MyFunction()
{
//bla bla bla do something
    PauseFor(2);

}

First run MyFunction() after first run , 2 seconds can't run.. after 2 seconds run again

Simply, a return false for 2 seconds

user5546877
  • 93
  • 1
  • 2
  • 7
  • 1
    use the `sleep` function - also: http://stackoverflow.com/questions/4184468/sleep-for-milliseconds – ewcz Dec 02 '15 at 07:50
  • The sleep function, it's different. I don't need sleep. Read again my question :D – user5546877 Dec 02 '15 at 07:51
  • The `clock` function is not a reliable timer function, and returns different things on different platforms. Besides, most operating systems contains native functions to put process to sleep for some amount of time, in a way that won't use CPU. See e.g. [`nanosleep`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html) on POSIX (e.g. Linux and OSX) platforms, or [`Sleep`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx) on Windows. – Some programmer dude Dec 02 '15 at 07:51
  • 4
    But your `PauseFor` (if it works) is the exact same thing as `sleep` (or `Sleep`), it cause the process to stop doing any work for around two seconds. The difference is that your function will max out the CPU core your process is running on which prevents other processes from using it. – Some programmer dude Dec 02 '15 at 07:55
  • @user5546877 Is your application multi-threaded? In other words: Could anyone call the function while it’s sleeping? If not, `sleep()` should suffice. Downvoted because of unclear question. – Melebius Dec 02 '15 at 07:55
  • Guys, sleep() pause executing for x seconds and the run automaticay... – user5546877 Dec 02 '15 at 08:01
  • I don't want to run after 2 seconds automaticaly, just when i call. that function – user5546877 Dec 02 '15 at 08:01
  • @user5546877 So what shall happen when the pause finishes? A program cannot just “do nothing” until you “trap” it using active wait (`while`) or something like `sleep()`. – Melebius Dec 02 '15 at 08:07
  • I don't know if I understand what you want, but do you mean that in youe example the `MyFunction` should return when you call `PauseFor` and let some other code run, and when the `PauseFor` function is done the `MyFunction` should resume after the `PauseFor` call? Then no that's not really possible in C++. – Some programmer dude Dec 02 '15 at 08:07

1 Answers1

1

Try this

std::this_thread::sleep_for(std::chrono::seconds(2));
Chen OT
  • 3,486
  • 2
  • 24
  • 46