ygramoel
ygramoel
DIIDevHeads IoT Integration Server
Created by Marvee Amasi on 4/25/2024 in #middleware-and-os
please I have a question
Sure. Sorry I did not explain immediately, I was too busy. First, let's discuss what "safe" means. The OP did not specify this. I assume that "safe" in this context means that the behavior is the same regardless of which compiler or optimization level you use, and also when another thread is running that can access ga at any time. I am not denying that in most cases, func_a will read ga exactly once. I am only saying that this is not guaranteed. If we cannot assume a specific compiler or optimization level, we can only rely on a C standard specification. No C standard that I know defines read instructions; this is left to the compiler implementation. The code generated by a compiler for a call to func_a or func_b might contain between 0 and 3 read instructions for the memory address containing ga. For func_b, it might decide to read ga once or three times. Even for func_a, it might decide to read ga multiple times (although this is not probable, it would be allowed by the C standard). Moreover, for both functions, it might decide to inline the function at a call site. (Both functions are global, so it will also have to keep a non-inline copy). If ga is already available in a register at the call site, the compiler might decide not to read it again, so there will be zero reads at the call site. It could also be the case that multiple read instructions are required to read ga. For example, if int is a 4-byte value, the code could be compiled for a microcontroller that cannot read 4 bytes with a single instruction. Admittedly, this is not a common situation with modern processors.
16 replies