我一直在寻找适合实时应用的轻量级跨平台睡眠功能(即具有可靠性的高分辨率/高精度)。以下是我的发现:
调度基础
放弃 CPU 然后将其取回是昂贵的。根据this article 的说法,Linux 上的调度程序延迟可能在 10-30 毫秒之间。因此,如果您需要以低于 10 毫秒的精度进行睡眠,那么您需要使用特殊的操作系统特定 API。通常的 C++11 std::this_thread::sleep_for 不是高分辨率睡眠。例如,在我的机器上,快速测试表明,当我要求它仅休眠 1ms 时,它通常会休眠至少 3ms。
Linux
最流行的解决方案似乎是 nanosleep() API。但是,如果您想要 alarms。
Windows
这里的解决方案是按照其他人的建议使用多媒体时间。如果你想在 Windows 上模拟 Linux 的 nanosleep(),下面是方法 (original ref)。同样,请注意,如果您在循环中调用 sleep(),则不需要一遍又一遍地执行 CreateWaitableTimer()。
#include <windows.h> /* WinAPI */
/* Windows sleep in 100ns units */
BOOLEAN nanosleep(LONGLONG ns){
/* Declarations */
HANDLE timer; /* Timer handle */
LARGE_INTEGER li; /* Time defintion */
/* Create timer */
if(!(timer = CreateWaitableTimer(NULL, TRUE, NULL)))
return FALSE;
/* Set timer properties */
li.QuadPart = -ns;
if(!SetWaitableTimer(timer, &li, 0, NULL, NULL, FALSE)){
CloseHandle(timer);
return FALSE;
}
/* Start & wait for timer */
WaitForSingleObject(timer, INFINITE);
/* Clean resources */
CloseHandle(timer);
/* Slept without problems */
return TRUE;
}
跨平台代码
这是为 Linux、Windows 和 Apple 平台实现睡眠的time_util.cc。但是请注意,它没有像我上面提到的那样使用 sched_setscheduler 设置实时模式,所以如果你想使用 example here。
#include "time_util.h"
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#else
# include <time.h>
# include <errno.h>
# ifdef __APPLE__
# include <mach/clock.h>
# include <mach/mach.h>
# endif
#endif // _WIN32
/**********************************=> unix ************************************/
#ifndef _WIN32
void SleepInMs(uint32 ms) {
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = ms % 1000 * 1000000;
while (nanosleep(&ts, &ts) == -1 && errno == EINTR);
}
void SleepInUs(uint32 us) {
struct timespec ts;
ts.tv_sec = us / 1000000;
ts.tv_nsec = us % 1000000 * 1000;
while (nanosleep(&ts, &ts) == -1 && errno == EINTR);
}
#ifndef __APPLE__
uint64 NowInUs() {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return static_cast<uint64>(now.tv_sec) * 1000000 + now.tv_nsec / 1000;
}
#else // mac
uint64 NowInUs() {
clock_serv_t cs;
mach_timespec_t ts;
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cs);
clock_get_time(cs, &ts);
mach_port_deallocate(mach_task_self(), cs);
return static_cast<uint64>(ts.tv_sec) * 1000000 + ts.tv_nsec / 1000;
}
#endif // __APPLE__
#endif // _WIN32
/************************************ unix <=**********************************/
/**********************************=> win *************************************/
#ifdef _WIN32
void SleepInMs(uint32 ms) {
::Sleep(ms);
}
void SleepInUs(uint32 us) {
::LARGE_INTEGER ft;
ft.QuadPart = -static_cast<int64>(us * 10); // '-' using relative time
::HANDLE timer = ::CreateWaitableTimer(NULL, TRUE, NULL);
::SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
::WaitForSingleObject(timer, INFINITE);
::CloseHandle(timer);
}
static inline uint64 GetPerfFrequency() {
::LARGE_INTEGER freq;
::QueryPerformanceFrequency(&freq);
return freq.QuadPart;
}
static inline uint64 PerfFrequency() {
static uint64 xFreq = GetPerfFrequency();
return xFreq;
}
static inline uint64 PerfCounter() {
::LARGE_INTEGER counter;
::QueryPerformanceCounter(&counter);
return counter.QuadPart;
}
uint64 NowInUs() {
return static_cast<uint64>(
static_cast<double>(PerfCounter()) * 1000000 / PerfFrequency());
}
#endif // _WIN32
另一个更完整的跨平台代码可以是found here。
另一个快速解决方案
您可能已经注意到,上面的代码不再是轻量级的。它需要包含 Windows 标头以及其他内容,如果您正在开发仅标头库,这可能不是非常理想的。如果您需要少于 2 毫秒的睡眠并且您不太热衷于使用操作系统代码,那么您可以使用以下简单的解决方案,它是跨平台的,并且在我的测试中效果很好。请记住,您现在没有使用经过高度优化的操作系统代码,这可能会更好地节省电力和管理 CPU 资源。
typedef std::chrono::high_resolution_clock clock;
template <typename T>
using duration = std::chrono::duration<T>;
static void sleep_for(double dt)
{
static constexpr duration<double> MinSleepDuration(0);
clock::time_point start = clock::now();
while (duration<double>(clock::now() - start).count() < dt) {
std::this_thread::sleep_for(MinSleepDuration);
}
}
相关问题