4

I can't find a viable guide so I am asking how do I delay a function in C, in other words, how do I make a program wait a certain number of seconds before continuing to execute other functions?

For example:

printf("First Function.\n");

//program waits a certain number of seconds before executing next command

printf("Second function executed after certain amount of seconds");

I want to know, what is the command I use to make this happen?

delay();

for me doesn't work by the way.

I must add, sleep() doesn't work either.

octagonlord69
  • 75
  • 2
  • 2
  • 6
  • 1
    You want `sleep()` – Chris Apr 29 '17 at 14:15
  • 2
    `sleep` on Unix, `Sleep` on Windows. – aschepler Apr 29 '17 at 14:16
  • 1
    I suggest you google the question title. – Weather Vane Apr 29 '17 at 14:18
  • 4
    There is no such capability in standard C. Solutions are specific to operating systems (or families of operating systems). For example, `sleep()` under unix, `Sleep()` under windows. You'll need to read the documentation for your target system and associated APIs. – Peter Apr 29 '17 at 14:21
  • 3
    "*`sleep()` doesn't work either.*" does not work in which sense? Please be more specific! On which platform are you experiencing these issues? – alk Apr 29 '17 at 15:39
  • This question is related to this question http://stackoverflow.com/q/14812233/694576 if not a duplicate to it. – alk Apr 29 '17 at 16:59
  • @alk: Despite your 'not a duplicate' comment, I think it is covering exactly the same territory, and so I've made it a duplicate. Whole-second vs sub-second sleeping is still sleeping. – Jonathan Leffler Jun 11 '17 at 00:54

4 Answers4

7

sleep is what you’re looking for.

printf("First Function.\n");

sleep(20);  // Replace 20 with the "certain amount"

printf("Second function executed after certain amount of seconds")
idmean
  • 14,540
  • 9
  • 54
  • 83
  • The latest documentation is here; http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sleep.html – alk Apr 30 '17 at 04:58
1

You can use Sleep() or select to add delay.

Pras
  • 4,047
  • 10
  • 20
  • 1
    I am curious why `sleep()` wouldn't be thread-safe. – Dolda2000 Apr 29 '17 at 14:17
  • I edited my answer, I dont know why but I am just used to call select for delays in threads, documentation for sleep clearly says only calling thread sleeps – Pras Apr 29 '17 at 14:24
  • Perhaps you were thinking of `pselect()`, which has nothing with thread-safety to do, but is async-signal safe. – Dolda2000 Apr 29 '17 at 14:25
1

The sleep() function is POSIX. There is also the POSIX function nanosleep() if more precision is needed.

There is the thrd_sleep() function in C11 (from threads.h), but AFAIK this function is still not supported by glibc.

A simple sleep function could be implemented portably using the functions from time.h:

#include <stdio.h>
#include <time.h>

void my_sleep(unsigned);
void delay_print(char *);

int main(void)
{
    delay_print("What is taking so long?");
    delay_print("Glad that's over!");
}

void my_sleep(unsigned duration)
{
    time_t start = time(NULL);
    double end = duration;
    time_t now;
    do {
        now = time(NULL);
    } while (difftime(now, start) < end);
}

void delay_print(char *msg)
{
    my_sleep(5);
    puts(msg);
}
ad absurdum
  • 19,498
  • 5
  • 37
  • 60
  • this sucks all the available CPU cycles due to the 'polling', so probably not the best approach. – user3629249 Apr 30 '17 at 18:03
  • @user3629249-- I did write "simple sleep function," and I think that it was implicit in my answer that this could be used in the _absence_ of any appropriate library functions. I think that most OS will detect the load and make adjustments. I am not sure that there is a portable way to do this and avoid CPU overhead, but I would be happy to see an alternative :) – ad absurdum Apr 30 '17 at 18:39
  • Conflictingly. I have minGW on windows 11 and eclipse findng `Sleep(10);` on `synchapi.h` and `sleep(10);` on `unistd.h`, why? – mohammadsdtmnd Nov 15 '22 at 18:51
  • @mohammadsdtmnd -- I don't understand "_conflictingly_". `synchapi.h` is a Windows-only header which declares a `Sleep` function; `unistd.h` is the Posix header that declares `sleep`. You can have both on the same system. – ad absurdum Nov 15 '22 at 19:56
0

You can easily use:

sleep(unsigned int seconds);   //   from the unistid.h
Sleep(int milliseconds);       //   from the Windows.h
phuclv
  • 37,963
  • 15
  • 156
  • 475