I found this function in a github repo which implements a lockfree queue. This function uses the QueryPerformanceCounter
to get accurate system time.
#define CompilerMemBar() std::atomic_signal_fence(std::memory_order_seq_cst)
SystemTime getSystemTime()
{
LARGE_INTEGER t;
CompilerMemBar();
if (!QueryPerformanceCounter(&t)) {
return static_cast<SystemTime>(-1);
}
CompilerMemBar();
return static_cast<SystemTime>(t.QuadPart);
}
I noticed there are two CompilerMemBar()
, which I thought is intented to prevent compiler from re-ordering. However, after I searched some codes on github, I found wrapping QueryPerformanceCounter
with compiler barriers may not be a common practice. So my question is these barriers here to handle some special situations? May be a possible reorder will affect the precision of system time we get? But I can't figure out how they will do, because I think even either the WINAPI call or the return statement is reordered, it seems to have no influence on the precision.