1、linux 系统环境下 有库函数 usleep
2、windows 系统环境下 没有usleep
3、windows 系统环境下模拟 linux 实现 usleep
#ifdef _WIN32
#include <windows.h>
#define sleep(sec) Sleep(sec * 1000)
#define msleep(msec) Sleep(msec)
static void usleep(unsigned long usec)
{
HANDLE timer;
LARGE_INTEGER interval;
interval.QuadPart = -(10 * usec);
timer = CreateWaitableTimer(NULL, TRUE, NULL);
SetWaitableTimer(timer, &interval, 0, NULL, NULL, 0);
WaitForSingleObject(timer, INFINITE);
CloseHandle(timer);
}
#else
#include <unistd.h>
#define msleep(msec) usleep(msec * 1000)
#endif
int main(int argc, char *argv[])
{
sleep(1);
msleep(100);
usleep(1000);
}