Cross-platform implementation of the pause x86 instruction

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() {
// 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
}
+4
source share
1 answer

Is this internal solution correct even if the native processor does not support the pause x86 instruction?

Yes, the pause instruction is encoded as F3 90. A pre Pentium 4 processor that does not know about this instruction will decode it as:

  REP NOP

NOP -. , . PAUSE, - , .

: REP NOP 8086, 35 . , .

+6

All Articles