将GetTickCount()vxWorks转换为Linux

2 c++ linux vxworks

我正在将一些vxWorks代码移植到Linux.

我看了这个答案,推荐CLOCK_MONOTONIC.这是以下定义的合适替代品:

#define GetTickCount()    ((1000.0 * (double)tickGet())/((double)sysClkRateGet())))
Run Code Online (Sandbox Code Playgroud)

Rob*_*obᵩ 10

GetTickCount是这样描述的一个Windows API:

检索自系统启动以来经过的毫秒数,最多49.7天

是的,CLOCK_MONOTONIC是正确使用的POSIX时钟.这是未经测试的代码:

double GetTickCount(void) 
{
  struct timespec now;
  if (clock_gettime(CLOCK_MONOTONIC, &now))
    return 0;
  return now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0;
}
Run Code Online (Sandbox Code Playgroud)