Skip to content

Commit eee496c

Browse files
committed
fix: use Sleep API on Windows
1 parent 96caf65 commit eee496c

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

examples/ffi/profiles.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#include <stdlib.h>
99
#include <string.h>
1010
#include <time.h>
11+
#ifdef _WIN32
12+
#define WIN32_LEAN_AND_MEAN
13+
#include <windows.h>
14+
#endif
1115

1216
/* This example simulates gathering wall and allocation samples. Roughly every
1317
* minute it will export the data, assumes an http://localhost:8126/ location
@@ -63,6 +67,18 @@ static struct ddog_Timespec now_wall(void) {
6367
return dd;
6468
}
6569

70+
// Cross-platform sleep with millisecond precision (sufficient for ~10ms accuracy)
71+
static void sleep_ms(unsigned int ms) {
72+
#ifdef _WIN32
73+
Sleep(ms);
74+
#else
75+
struct timespec req;
76+
req.tv_sec = ms / 1000;
77+
req.tv_nsec = (long)((ms % 1000) * 1000000L);
78+
nanosleep(&req, NULL);
79+
#endif
80+
}
81+
6682
static int64_t rand_range_i64(int64_t min_inclusive, int64_t max_inclusive) {
6783
if (max_inclusive <= min_inclusive)
6884
return min_inclusive;
@@ -284,8 +300,7 @@ int main(void) {
284300
}
285301

286302
// Sleep ~10ms, obviously this will drift, this is just an example.
287-
struct timespec req = {.tv_sec = 0, .tv_nsec = WALL_TICK_NS};
288-
nanosleep(&req, NULL);
303+
sleep_ms((unsigned)(WALL_TICK_NS / 1000000));
289304
}
290305

291306
printf("[profiles.c] shutting down\n");

0 commit comments

Comments
 (0)