Friday, 14 December 2018

c++ - CPU Relax instruction and C++11 primitives

I've noticed that many lockless algorithms implemented using OS-specific primitives, such as the spin locks described here (which use Linux-specific atomic primitives) often make use of a "cpu relax" instruction. With GCC, this can be achieved with:



asm volatile("pause\n": : :"memory");



Specifically, this instruction is often used in the body of while loop spin locks, while waiting for a variable to set to a certain value.



C++11 doesn't seem to provide any kind of portable "cpu_relax" type instruction. Is there some reason for this? And does the "pause" statement actually accomplish anything useful?



Edit:



Also, I'd ask: why did the C++11 standards committee not decide to include a generic std::cpu_relax() or whatever? Is it too difficult to guarantee portability?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...