What is the best practice to write a cross platform implementation of the x86 pause instruction? I am planning to use it in a busy spinning loop in a C++ 11 project.
If I was only using the gcc tool-chain then I could use the _mm_pause intrinsic. Does this intrinsic do the right thing even when the native processor does not support the x86 pause instruction? I would also like my code to work on the clang/llvm tool-chain too.
I imagine that a fallback could use "std::this_thread::sleep_for" since I am using C++ 11. But I am not sure how to detect processor capability (supports pause vs does not) and fall back to sleep.
I am using cmake to build my project and also will always build and deploy on the same machine. So I am comfortable detecting processor settings during compilation.
An example implementation (pseudocode) is :
void pause() {
// Not sure how to detect if pause is available on the platform.
#if defined(USE_mm_pause)
__asm__ ( "pause;" );
#else
std::this_thread::sleep_for(std::chrono::seconds(0));
#endif
}