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.
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).