/**
* 获取当前时间的tick数(以毫秒为单位)
*
* 本函数不接受参数。
*
* @return 返回当前时间的毫秒级tick数。返回值为uint32_t类型。
*/
#include <stdio.h>
#include <sys/types.h>
static u_int32_t getTickCountMs
{
struct timespec ts = {0,0}; // 初始化一个timespec结构体为0
clock_gettime(CLOCK_MONOTONIC,&ts); // 获取系统当前时间,填充到ts中
return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);// 将秒数转换为毫秒,并将纳秒数除以1,000,000转换为毫秒,然后相加
}
void test()
{
static u_int32_t start_100ms_delay = 0;
start_100ms_delay = getTickCountMs();
u_int32_t end_100ms_delay = getTickCountMs();
if(end_100ms_delay - start_100ms_delay >= 100)
{
printf("more than 100ms");
start_100ms_delay = end_100ms_delay;
}
}