8

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
}
5
  • The way to detect the instruction's availability would be to write a small program that uses it, and then compile, link, and run that program during your compilation process. Either it is an acceptable part of the ISA to the toolchain and hardware, or you get a failed compiler or a SIGILL for an illegal instruction. Nov 5, 2013 at 0:00
  • 1
    I would prefer __asm__ __volatile__("pause;"); so that GCC will not reorder memory accesses across the pause.
    – Casey
    Nov 5, 2013 at 5:12
  • Does sleep_for relate to yield in any way, or it's a coincidence, as for sleep/yield the scheduler will take care of your time slice, while nop is just a nop.
    – Red XIII
    Nov 5, 2013 at 10:31
  • 1
    @RedXIII Was that a question? std::this_thread::sleep_for will run the sleep command which will just put the current thread to sleep but not schedule another thread to run on the core. For yield, C++ 11 has the std::this_thread::yield command. This allows the scheduler the opportunity to reschedule.
    – Rajiv
    Nov 5, 2013 at 19:45
  • 1
    Use _mm_pause() from <immintrin.h>. It's portable to all the major compilers including MSVC. See How do you use the pause assembly instruction in 64-bit C++ code?. (tempted to close this as a duplicate.) Aug 18, 2018 at 12:44

1 Answer 1

13

Does this intrinsic do the right thing even when the native processor does not support the x86 pause instruction?

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

  REP NOP

That is just a ordinary NOP with a useless prefix-byte. The processor will wait a cycle or two and then continue without altering the processor state in any way. You will not get the performance and power benefits from using PAUSE but the program will still work as expected.

Fun fact: REP NOP was even legal on the 8086 released roughly 35 years ago. That's what I call backward compatibility.

2
  • Thanks for the answer. That seems like a good solution for gcc. Any comment on support on other compilers, especially clang?
    – Rajiv
    Nov 5, 2013 at 18:12
  • I was using the "xmmintrin.h" header before. I have switched to "x86intrin.h" now. Seems like it's working on gcc 4.3 and clang 3.3.1
    – Rajiv
    Nov 5, 2013 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.