feat: add sleep ms function

This commit is contained in:
2025-09-03 20:33:17 +02:00
parent f467275b84
commit 77e58d1578
2 changed files with 30 additions and 0 deletions

24
src/sleepms.c Normal file
View File

@@ -0,0 +1,24 @@
#include "sleepms.h"
#ifdef WIN32
#include <windows.h>
#elif _POSIX_C_SOURCE >= 199309L
#include <time.h> // for nanosleep
#else
#include <unistd.h> // for usleep
#endif
void sleep_ms(int milliseconds){ // cross-platform sleep function
#ifdef WIN32
Sleep(milliseconds);
#elif _POSIX_C_SOURCE >= 199309L
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#else
if (milliseconds >= 1000)
sleep(milliseconds / 1000);
usleep((milliseconds % 1000) * 1000);
#endif
}

6
src/sleepms.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef SLEEPMS_H
#define SLEEPMS_H
void sleep_ms(int milliseconds);
#endif // SLEEPMS_H