Skip to main content
add indents; sleep() takes seconds (not ms, https://man7.org/linux/man-pages/man3/sleep.3.html)
Source Link

I need to wait for short intervals of time in my program, but I really don't want to include unistd.h just to use sleep, so I'm doing it this way:

int main()
{
    for(int i=0; i<=100000000000; i++); // <----- note that the loop doesn't do anything 
    return 0;
}

I do this when I don't really have a specific interval to set for sleep.

Is there anything wrong with this? Would this harm the program in any way?

In C I know that if I did include unistd.h, and that if I used sleep(), that the following code would also make the program wait for a short interval of time:

int main()
{
    sleep(20002); //or whatever the number (in millisecondsseconds)
    return 0;
}

I need to wait for short intervals of time in my program, but I really don't want to include unistd.h just to use sleep, so I'm doing it this way:

int main()
{
for(int i=0; i<=100000000000; i++); // <----- note that the loop doesn't do anything 
return 0;
}

I do this when I don't really have a specific interval to set for sleep.

Is there anything wrong with this? Would this harm the program in any way?

In C I know that if I did include unistd.h, and that if I used sleep(), that the following code would also make the program wait for a short interval of time:

int main()
{
sleep(2000); //or whatever the number (in milliseconds)
return 0;
}

I need to wait for short intervals of time in my program, but I really don't want to include unistd.h just to use sleep, so I'm doing it this way:

int main()
{
    for(int i=0; i<=100000000000; i++); // <----- note that the loop doesn't do anything 
    return 0;
}

I do this when I don't really have a specific interval to set for sleep.

Is there anything wrong with this? Would this harm the program in any way?

In C I know that if I did include unistd.h, and that if I used sleep(), that the following code would also make the program wait for a short interval of time:

int main()
{
    sleep(2); //or whatever the number (in seconds)
    return 0;
}
edited title
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Using a for-loop instead ofor sleeping? to wait for short intervals of time

Tweeted twitter.com/#!/StackCodeReview/status/443653503982907393
Post Reopened by 200_success, CommunityBot, rolfl, Vogel612, Jeff Vanzella
Make the actual code being used the focus of the question. The sleep() code is a by-the-way
Source Link
rolfl
  • 98.1k
  • 17
  • 220
  • 419

In C, using sleep() would make the programI need to wait for a short intervalintervals of time:

int main()
{
sleep(2000); //or whatever the number (in milliseconds)
return 0;
}

But in my program, but I really don't want to include unistd.h just to use sleep, so I'm doing it this way:

int main()
{
for(int i=0; i<=100000000000; i++); // <----- note that the loop doesn't do anything 
return 0;
}

I do this when I don't really have a specific interval to set for sleep.

Is there anything wrong with this? Would this harm the program in any way?

In C I know that if I did include unistd.h, and that if I used sleep(), that the following code would also make the program wait for a short interval of time:

int main()
{
sleep(2000); //or whatever the number (in milliseconds)
return 0;
}

In C, using sleep() would make the program wait for a short interval of time:

int main()
{
sleep(2000); //or whatever the number (in milliseconds)
return 0;
}

But I really don't want to include unistd.h just to use sleep, so I'm doing it this way:

int main()
{
for(int i=0; i<=100000000000; i++); // <----- note that the loop doesn't do anything 
return 0;
}

I do this when I don't really have a specific interval to set for sleep.

Is there anything wrong with this? Would this harm the program in any way?

I need to wait for short intervals of time in my program, but I really don't want to include unistd.h just to use sleep, so I'm doing it this way:

int main()
{
for(int i=0; i<=100000000000; i++); // <----- note that the loop doesn't do anything 
return 0;
}

I do this when I don't really have a specific interval to set for sleep.

Is there anything wrong with this? Would this harm the program in any way?

In C I know that if I did include unistd.h, and that if I used sleep(), that the following code would also make the program wait for a short interval of time:

int main()
{
sleep(2000); //or whatever the number (in milliseconds)
return 0;
}
Post Closed as "Not suitable for this site" by 200_success, Michael Urman, Jamal
Question Protected by rolfl
added 4 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Better title.
Link
syb0rg
  • 21.9k
  • 10
  • 113
  • 193
Loading
added 2 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
added 5 characters in body
Source Link
Mohammad
  • 1.3k
  • 1
  • 10
  • 29
Loading
Source Link
Mohammad
  • 1.3k
  • 1
  • 10
  • 29
Loading