1
0
mirror of synced 2025-12-15 09:38:11 +08:00

!18 !optimize cpu used

* !optimize 时间追平漂移量时,损耗一点时间(微妙级别)降低CPU占用
This commit is contained in:
微希夷
2022-06-15 07:40:48 +00:00
committed by yitter
parent 5e24340526
commit 1f13dca448

View File

@@ -4,6 +4,7 @@
#else
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#endif
#include <stdlib.h>
#include <stdio.h>
@@ -221,7 +222,6 @@ static inline uint64_t GetSysCurrentTime()
gettimeofday(&t, NULL);
return (uint64_t)(t.tv_sec * 1000 + t.tv_usec / 1000);
#endif
}
static inline uint64_t GetCurrentTimeTick(snowflake *flake)
@@ -232,9 +232,21 @@ static inline uint64_t GetCurrentTimeTick(snowflake* flake)
static inline uint64_t GetNextTimeTick(snowflake *flake)
{
uint64_t tempTimeTicker = GetCurrentTimeTick(flake);
while (tempTimeTicker <= flake->_LastTimeTick)
struct timespec delay;
delay.tv_sec = 0;
delay.tv_nsec = 500000;
while (1)
{
tempTimeTicker = GetCurrentTimeTick(flake);
if (tempTimeTicker > flake->_LastTimeTick)
{
break;
}
#ifdef WIN32
SwitchToThread();
#else
nanosleep(&delay, NULL);
#endif
}
return tempTimeTicker;
}