我有运行循环的线程。 我需要该循环每 5 毫秒运行一次(1 毫秒错误)。 我知道 Sleep() 函数并不精确。
你有什么建议吗?
更新。 我不能用其他方式做到这一点。 在循环结束时,我需要某种 sleep 。 我也不希望 CPU 占用 100%。
最佳答案
我一直在寻找适用于实时应用(即高分辨率/高精度和可靠性)的轻量级跨平台休眠功能。以下是我的发现:
调度基础知识
放弃 CPU 然后再取回它是昂贵的。根据this article ,调度程序延迟在 Linux 上可能在 10-30 毫秒之间。因此,如果您需要高精度地休眠少于 10 毫秒,那么您需要使用特定于操作系统的特殊 API。通常的 C++11 std::this_thread::sleep_for 不是高分辨率 sleep 。例如,在我的机器上,快速测试表明,当我要求它仅休眠 1 毫秒时,它通常至少休眠 3 毫秒。
Linux
最流行的解决方案似乎是 nanosleep() API。但是,如果您想要 < 2ms 的高分辨率 sleep 时间,则还需要使用 sched_setscheduler 调用来设置线程/进程以进行实时调度。如果你不这样做,那么 nanosleep() 就像过时的 usleep 一样,其分辨率为 ~10ms。另一种可能性是使用 alarms .
window
这里的解决方案是像其他人建议的那样使用多媒体时间。如果你想在 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;
}
跨平台代码
这是 time_util.cc它为 Linux、Windows 和 Apple 的平台实现 sleep 。但是请注意,它不会像我上面提到的那样使用 sched_setscheduler 设置实时模式,所以如果你想使用 <2ms,那么这是你需要额外做的事情。您可以进行的另一项改进是,如果您在某个循环中调用 sleep,则避免一遍又一遍地调用适用于 Windows 版本的 CreateWaitableTimer。有关如何执行此操作,请参阅 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 header 以及其他内容,如果您正在开发仅包含 header 的库,这可能不是很理想。如果您需要少于 2 毫秒的 sleep 并且您不太热衷于使用操作系统代码,那么您可以使用以下简单的跨平台解决方案,并且在我的测试中运行良好。请记住,您现在没有使用高度优化的操作系统代码,这可能会更好地节省电量和管理 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);
}
}
相关问题
关于c++ - 需要精确的线程休眠。最大 1ms 误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13397571/