1

Many people told me there is a better way to create a delay for X amount of milliseconds. People told me about sleep command and usleep(), but I failed to make that work.

Currently I'm using this:

void delay(unsigned int mseconds) {
    clock_t goal=mseconds+clock();
    while(goal>clock());
}

And by doing this

delay(500);
printf("Hello there");

I can make text appear half a second later, but I want to find a better way to do this since people told me this is a bad method to do it and that it can be not very accurate.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 6
    In which way did your attempts to use sleep() and usleep() not work? – Yunnosch Jul 29 '18 at 20:13
  • 1
    Are you aware of the meaning of "busy waiting"? It describes the problem caused by your method more accuratly than "not very accurate". – Yunnosch Jul 29 '18 at 20:14
  • Pausing execution is not a language feature but typically an operating system primitive. Most C standard libraries contains a sleep function, but precision and granularity is historically poor. It would improve your question if you stated *why* you want the program to pause. – wallyk Jul 29 '18 at 20:14
  • https://stackoverflow.com/questions/14818084/what-is-the-proper-include-for-the-function-sleep-in-c/14818104 – Terry Carmen Jul 29 '18 at 20:15
  • @wallyk I want to use it to make things appear slowly, basically using `printf("Waiting"); for(i=0;i<3;i++){ printf("."); delay(500)}` –  Jul 29 '18 at 20:39
  • @Yunnosch basically just by typing them between stuff... –  Jul 29 '18 at 20:41
  • Is that last comment supposed to answer one of my questions? I do not see a connection. – Yunnosch Jul 29 '18 at 20:42
  • @Yunnosch sorry didn't read them fully, my bad, basically compiler gave errors that those things don't exist –  Jul 29 '18 at 20:45
  • 1
    Please quote the compiler messages in your question. Do not forget to completly list all of your includes. You probably also want to state your environment; OS, compiler, version, ... – Yunnosch Jul 29 '18 at 20:54
  • @Yunnosch sorry to take everyones time, made it work (answer down below). –  Jul 29 '18 at 20:57

2 Answers2

1

I figured everything out!!! God I was dump when trying to make sleep() command work first time

#include<stdio.h>
#include<windows.h>

int main(){

    int load;
    int Loadwait=100

    for(load=0;load<100;load+=5){
        printf("Loading %d",load);
        Sleep(Loadwait);
        system("cls");
    }
    return 0;
}
0

Using musl libc you should be able to use thrd_sleep().

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

int main(void)
{
    printf("Time: %s", ctime(&(time_t){time(NULL)}));
    thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec
    printf("Time: %s", ctime(&(time_t){time(NULL)}));
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • `threads.h` is optional in `C11` and many don't support it, as POSIX has `pthreads` and Windows has `windows.h`. At least use `__STDC_NO_THREADS__` to check, if it is available. – Inrin Jul 29 '18 at 21:03
  • It's not really about the compiler, but which `libc` it uses. For example on most Linuces _glibc_ is used, which sadly doesn't support `threads.h` but _musl_ on the other hand supports it. It's really a shame the standard made it optional. tested with clang 6.0.1 / gcc 8.1.1 and glib 2.27 / musl 1.1.19 – Inrin Jul 29 '18 at 21:27