Reputation:
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.
Upvotes: 1
Views: 192
Reputation: 13134
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)}));
}
Upvotes: 0
Reputation:
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;
}
Upvotes: 1