Marvee Amasi
Marvee Amasi
DIIDevHeads IoT Integration Server
Created by Marvee Amasi on 4/25/2024 in #middleware-and-os
please I have a question
Hi guys please I have this question

// ga is a global variable

int func_a() {
int a = ga;
return a>2 ? a-2 : 2-a;
}

int func_b() {
return ga>2 ? ga-2 : 2-ga;
}

// ga is a global variable

int func_a() {
int a = ga;
return a>2 ? a-2 : 2-a;
}

int func_b() {
return ga>2 ? ga-2 : 2-ga;
}
Are these two code snippets functionally identical in a multi-threaded or real-time operating system (RTOS) environment? Specifically, is the first code guaranteed to safely read the global variable ga only once, ensuring consistent behavior even if ga changes? This is not about data protection mechanisms like locks, but rather compiler behavior regarding register assignment and variable reading. @Middleware & OS
16 replies