Is there a good way to slow down program output? usleep and nanosleep don't slow it down enough, delay doesn't work, and sleep keeps freezing my program. I am using Linux since I think this makes a difference in what I have to use.

To address the question directly, I would want to know, how long are you setting sleep(). usleep(), and nanosleep() for, repsectively? While sleep() takes an argument in seconds, usleep() takes one in microseconds, and nanosleep() (as the name implies) takes a structure that has both seconds and nanoseconds. if you are using sleep() with an argument more than 60, then it would indeed seem as if the program has frozen; OTOH, an argument of 99999 for usleep() (just under 1/10 of a second) would be barely noticable, as would any nonoseconds argument less than 99999999 (again, just under 1/10 of a second) for nanosleep().

To get the resolution you want, you may need to make multiple calls to nanosleep(), though that will still give you variable results as it does not guarantee a specific amount of time slept, only a minimum time.

You might also consider using timer_create() instead.

That having been said, what is the real purpose here? Why do you need to slow down the output, and what kind of output is it? Is this a delay for a video game or animation of some kind (in which case you may find a more suitable function in the graphics package you are using)?

I looked at the man pages and saw those were the maximum values of usleep() and nanosleep(). With sleep I thought I would get a normal 5 seconds. I am working on a modified version of game of life. It is hard to see if it is doing what I want with how fast it is going.

This article has been dead for over six months. Start a new discussion instead.