Design Pattern Examples
Overview of object-oriented design patterns
sleep.c
Go to the documentation of this file.
1
6
7#include <stdlib.h>
8
9#ifdef _MSC_VER
10#include <windows.h>
11#elif _POSIX_C_SOURCE >= 199309L
12#include <time.h> // for nanosleep
13#else
14#include <unistd.h> // for usleep
15#endif
16
17#include "sleep.h"
18
19
20// Sleep for the specified number of milliseconds.
21void sleep(int milliseconds)
22{
23#ifdef _MSC_VER
24 Sleep(milliseconds);
25#elif _POSIX_C_SOURCE >= 199309L
26 struct timespec ts;
27 ts.tv_sec = milliseconds / 1000;
28 ts.tv_nsec = (milliseconds % 1000) * 1000000;
29 nanosleep(&ts, NULL);
30#else
31 if (milliseconds >= 1000)
32 sleep(milliseconds / 1000);
33 usleep((milliseconds % 1000) * 1000);
34#endif
35}
36
Declaration of the sleep function, for sleeping for a number of milliseconds.
void sleep(int milliseconds)
Sleep for the specified number of milliseconds. Does not return until after the sleep period.
Definition: sleep.c:21