Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent g++ from optimizing out a loop controlled by a variable that can be changed by an IRQ?

Tags:

People also ask

How do I stop compiler optimization?

Use -O0 to disable them and use -S to output assembly. -O3 is the highest level of optimization. Starting with gcc 4.8 the optimization level -Og is available. It enables optimizations that do not interfere with debugging and is the recommended default for the standard edit-compile-debug cycle.

What is #pragma optimize?

The optimize pragma must appear outside a function. It takes effect at the first function defined after the pragma is seen. The on and off arguments turn options specified in the optimization-list on or off. The optimization-list can be zero or more of the parameters shown in the following table.


Consider the following piece of code:

unsigned global; while(global); 

global is modified in a function which is invoked by an IRQ. However, g++ removes the "is-not-zero" test and translates the while loop into an endless loop.

Disabling the compiler optimization solves the problem, but does C++ offer a language construct for it?