Cross-platform implementation of the x86 pause instruction

后端 未结 1 1376
萌比男神i
萌比男神i 2021-01-12 17:16

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.

1条回答
  •  离开以前
    2021-01-12 17:30

    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.

    0 讨论(0)
提交回复
热议问题