I saw asm("pause") in other people's code and I wonder what it does. The code is compiled by g++ on Linux.
This line is in a loop that's in another thread, which constantly polls if an update happens. I suspect it makes the program pauses a bit before polling again, but I wonder (1) Is my guess correct (2) why is it necessary to pause? The machine we run the code on has many processors and I the thread would totally just keep polling it.
See Jeff Preshing's Memory Ordering at Compile Time article. Making the asm instruction part non-empty also means those asm instructions will run every time the C logically runs that source line (because it's volatile ). pause prevents speculative loads from causing memory-ordering mis-speculation pipeling clears (aka machine nukes).
Making the asm instruction part non-empty also means those asm instructions will run every time the C logically runs that source line (because it's volatile ). pause prevents speculative loads from causing memory-ordering mis-speculation pipeling clears (aka machine nukes).
pause prevents speculative loads from causing memory-ordering mis-speculation pipeling clears (aka machine nukes). It's useful inside spin loops that are waiting to see a value in memory. You might find this statement inside a spinloop written without C++11 std::atomic, to tell the compiler it has to re-read the value of a global variable.
Basically that is called a spin loop, or busy wait. It would consume as much CPU resources as it can. This wastes CPU processing power and increases power consumption.
By putting pause instruction, you're hinting the processor that "this is a spin loop." This forces the processor not to be too smart to make unnecessary predictions (optimizations). Also, it frees up CPU time to be used for other things in some cases (e.g. Hyperthreading).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With