Any macro or tech piece to optimize parts?

I am working on a locking framework with the g ++ compiler. It looks like with the -o1 switch g ++ will change the order of my code execution. How can I disallow g ++ optimizations on a certain part of my code while supporting optimizations elsewhere? I know I can split it into two files and link them, but it looks ugly.

0


source to share


2 answers


You can use the function attribute __attribute__ ((optimize 0)) "to specify optimization for a single function or" #pragma GCC optimize "for a block of code. This is for GCC 4.4 only, though I think check your GCC manual. If they are not are supported, source separation is your only option.



I would also say that if your code does not work with optimization, most likely your code is simply wrong, especially since you are trying to do something fundamentally very difficult. The processor will potentially perform a reordering of your code (within sequential consistency), so any reordering you get with GCC could potentially happen.

+3


source


If you find that gcc changes the execution order in your code, you should use a memory barrier. Just don't assume that volatile variables will protect you from this problem. They will only make sure that on one thread the behavior is what the language guarantees, and will always read variables from their memory location to account for changes "invisible" to the executable code. (for example, changing a variable executed by a signal handler).

GCC has supported OpenMP since version 4.2. You can use it to create a memory barrier with a custom directive #pragma

.



A very good insight into blocking free code is a PDF from Herb Sutter and Andrei Alexandrescu: C ++ and the dangers of double checked blocking

+3


source







All Articles