利用select函数的定时返回功能在Windows上实现微秒级的cpu休眠

说明:在调用该函数前要先调用WSAStartup初始化Winsock

void uSleep(uint32_t usec)
{
    struct timeval timeout;
    fd_set fds;
    FD_ZERO(&fds);
    int32_t fd = socket(AF_INET, SOCK_DGRAM, 0);
    FD_SET(fd, &fds);
    timeout.tv_sec = usec / 1000000;
    timeout.tv_usec = usec % 1000000;
    int ret = select(0, NULL, NULL, &fds, &timeout);
    if (0 > ret)
    {
        perror("select");
    }
    closesocket(fd);
}

 

posted @ 2016-07-31 13:28  你好阿汤哥  Views(1161)  Comments(0Edit  收藏  举报