delay (wait)

May 12, 2010 at 5:09pm
I need to delay execution of some parts of my codes. I am Ubuntu user so, I can not use <windows.h> and its sleep(minutes) function. The codes which i am using is working but it uses actual loop and one core of my CPU. I want to delay (sleep) my codes tens of minutes or even hour's.

So, I am looking for other solution which will be less suffering for my CPU.

Any help would be appreciating.

Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <ctime>
//.......

void wait(double sec, double min, double h)
{

    double secs;
    secs=sec+60.0*min+3600.0*h;

    clock_t delay=secs * CLOCKS_PER_SEC; 
    clock_t start=clock();
    while(clock()-start<delay);  
}


May 12, 2010 at 5:14pm
usleep() or nanosleep().
May 12, 2010 at 9:29pm
usleep() and nanosleep() will have the same effect on the CPU.
May 13, 2010 at 1:57am
May 13, 2010 at 1:58am
/desk

Why not use the standard library's clock function?

-Albatross
May 13, 2010 at 2:17am
There have been a lot of threads concerning timers/delays lately.
May 13, 2010 at 2:46am
I still don't know why everyone's using delay(), sleep(), or Sleep() when they can write a function that's cross-platform and maybe not as generally awesome as initially presumed after a small discovery regarding the nature of those functions.

-Albatross
Last edited on May 13, 2010 at 2:59am
May 13, 2010 at 2:53am
Like this?
http://www.cplusplus.com/forum/unices/10491/#msg49054

When platform-specific code does it better, use it.
The whole point of sleep functions is to yield execution time to other processes. Using <ctime> stuff just puts you in a loop that eats processor time.
May 13, 2010 at 2:54am
Because sometimes it's desirable to yield CPU time?
And looping while checking clock() is anything but awesome.
May 13, 2010 at 2:57am
@Duoas:
That's... not what I meant.

I disagree with the fundamental statement behind your statement "When platform-specific code does it better, use it", however if indeed the sleep functions return execution time to other processes for a period, then indeed I will admit that they are a better solution than ticking off clocks for the purposes of not doing anything for a specific period of time.

-Albatross
May 13, 2010 at 2:58am
What?

Make up your mind.
May 13, 2010 at 3:02am
My mind is made up. I don't fully agree with your statement command "When platform-specific code does it better, use it", however if what you said is true about the sleep functions, and they do return some time to other processes, then for the purposes of doing exactly nothing for a few to a few trillion clock ticks, using those functions is a better solution than using clock() over and over again until it passes a certain value.

-Albatross
Last edited on May 13, 2010 at 3:03am
May 13, 2010 at 3:16am
they are a better solution than ticking off clocks for the purposes of not doing anything for a specific period of time
That's not necessarily true. It depends on how fast the scheduler is and how long you need to wait.
May 13, 2010 at 7:36am
Albatross and Duoas, you are arguing the same point. You're both saying to use [S/s]leep() instead of using clock() but Duoas is saying to create a wrapper function like this:
http://www.cplusplus.com/forum/unices/10491/#msg49054

And yes, busywaiting on clock() to return a particular value is a bad idea.
Last edited on May 13, 2010 at 7:37am
May 14, 2010 at 11:50pm
thanks a lot!
usleep() works fine.
Topic archived. No new replies allowed.