1
0
mirror of synced 2025-12-15 01:28:09 +08:00

!optimize 时间追平漂移量时,损耗一点时间(微妙级别)降低CPU占用

This commit is contained in:
Albert
2022-06-15 15:19:58 +08:00
parent 5e24340526
commit 3c15cf9df2

View File

@@ -4,6 +4,7 @@
#else #else
#include <unistd.h> #include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#include <time.h>
#endif #endif
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@@ -221,7 +222,6 @@ static inline uint64_t GetSysCurrentTime()
gettimeofday(&t, NULL); gettimeofday(&t, NULL);
return (uint64_t)(t.tv_sec * 1000 + t.tv_usec / 1000); return (uint64_t)(t.tv_sec * 1000 + t.tv_usec / 1000);
#endif #endif
} }
static inline uint64_t GetCurrentTimeTick(snowflake *flake) 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) static inline uint64_t GetNextTimeTick(snowflake *flake)
{ {
uint64_t tempTimeTicker = GetCurrentTimeTick(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); tempTimeTicker = GetCurrentTimeTick(flake);
if (tempTimeTicker > flake->_LastTimeTick)
{
break;
}
#ifdef WIN32
SwitchToThread();
#else
nanosleep(&delay, NULL);
#endif
} }
return tempTimeTicker; return tempTimeTicker;
} }