What is the best practice for writing a cross-platform implementation of the pause x86 instruction? I plan to use it in a closed loop loop in a C ++ 11 project.
If I only used the gcc toolchain, then I could use the built-in _mm_pause. Is this internal error correct even if the native processor does not support the pause x86 instruction? I would also like my code to work with the clang / llvm toolchain too.
I assume that the backup might use "std :: this_thread :: sleep_for" since I am using C ++ 11. But I am not sure how to detect the capabilities of the processor (supports pause vs is not) and is returning to sleep mode.
I use cmake to create my project and will always build and deploy on the same computer. Therefore, it is convenient for me to detect processor settings at compile time.
Implementation example (pseudocode):
void pause() {
#if defined(USE_mm_pause)
__asm__ ( "pause;" );
#else
std::this_thread::sleep_for(std::chrono::seconds(0));
#endif
}
Rajiv source
share